mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	First pass at removing some of the Todo modal code duplication
This commit is contained in:
		
							parent
							
								
									da75e116c1
								
							
						
					
					
						commit
						55b2490cc9
					
				
							
								
								
									
										105
									
								
								todo/widget.go
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								todo/widget.go
									
									
									
									
									
								
							@ -60,73 +60,37 @@ func (widget *Widget) editItem() {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// FIXME: Remove all this duplication
 | 
						form := widget.modalForm("Edit:", widget.list.Selected().Text)
 | 
				
			||||||
	_, _, w, h := widget.View.GetInnerRect()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	form := tview.NewForm().
 | 
						saveFctn := func() {
 | 
				
			||||||
		SetButtonsAlign(tview.AlignCenter).
 | 
							text := form.GetFormItem(0).(*tview.InputField).GetText()
 | 
				
			||||||
		SetButtonTextColor(tview.Styles.PrimaryTextColor)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	form.AddInputField("Edit item:", widget.list.Selected().Text, 60, nil, nil)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	form.AddButton("Save", func() {
 | 
					 | 
				
			||||||
		fld := form.GetFormItem(0)
 | 
					 | 
				
			||||||
		text := fld.(*tview.InputField).GetText()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		widget.list.Update(text)
 | 
							widget.list.Update(text)
 | 
				
			||||||
		widget.persist()
 | 
							widget.persist()
 | 
				
			||||||
		widget.pages.RemovePage("modal")
 | 
							widget.pages.RemovePage("modal")
 | 
				
			||||||
		widget.app.SetFocus(widget.View)
 | 
							widget.app.SetFocus(widget.View)
 | 
				
			||||||
		widget.display()
 | 
							widget.display()
 | 
				
			||||||
	})
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	form.AddButton("Cancel", func() {
 | 
						widget.addButtons(form, saveFctn)
 | 
				
			||||||
		widget.pages.RemovePage("modal")
 | 
						widget.modalFocus(form)
 | 
				
			||||||
		widget.app.SetFocus(widget.View)
 | 
					 | 
				
			||||||
		widget.display()
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
 | 
					 | 
				
			||||||
	frame.SetBorder(true)
 | 
					 | 
				
			||||||
	frame.SetRect(w+20, h+2, 80, 7)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	widget.pages.AddPage("modal", frame, false, true)
 | 
					 | 
				
			||||||
	widget.app.SetFocus(frame)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) newItem() {
 | 
					func (widget *Widget) newItem() {
 | 
				
			||||||
	// FIXME: Remove all this duplication
 | 
						form := widget.modalForm("New:", "")
 | 
				
			||||||
	_, _, w, h := widget.View.GetInnerRect()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	form := tview.NewForm().
 | 
						saveFctn := func() {
 | 
				
			||||||
		SetButtonsAlign(tview.AlignCenter).
 | 
							text := form.GetFormItem(0).(*tview.InputField).GetText()
 | 
				
			||||||
		SetButtonTextColor(tview.Styles.PrimaryTextColor)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	form.AddInputField("New item:", "", 60, nil, nil)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	form.AddButton("Save", func() {
 | 
					 | 
				
			||||||
		fld := form.GetFormItem(0)
 | 
					 | 
				
			||||||
		text := fld.(*tview.InputField).GetText()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		widget.list.Add(text)
 | 
							widget.list.Add(text)
 | 
				
			||||||
		widget.persist()
 | 
							widget.persist()
 | 
				
			||||||
		widget.pages.RemovePage("modal")
 | 
							widget.pages.RemovePage("modal")
 | 
				
			||||||
		widget.app.SetFocus(widget.View)
 | 
							widget.app.SetFocus(widget.View)
 | 
				
			||||||
		widget.display()
 | 
							widget.display()
 | 
				
			||||||
	})
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	form.AddButton("Cancel", func() {
 | 
						widget.addButtons(form, saveFctn)
 | 
				
			||||||
		widget.pages.RemovePage("modal")
 | 
						widget.modalFocus(form)
 | 
				
			||||||
		widget.app.SetFocus(widget.View)
 | 
					 | 
				
			||||||
		widget.display()
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
 | 
					 | 
				
			||||||
	frame.SetBorder(true)
 | 
					 | 
				
			||||||
	frame.SetRect(w+20, h+2, 80, 7)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	widget.pages.AddPage("modal", frame, false, true)
 | 
					 | 
				
			||||||
	widget.app.SetFocus(frame)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (widget *Widget) init() {
 | 
					func (widget *Widget) init() {
 | 
				
			||||||
@ -233,3 +197,48 @@ func (widget *Widget) persist() {
 | 
				
			|||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* -------------------- Modal Form -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) addButtons(form *tview.Form, saveFctn func()) {
 | 
				
			||||||
 | 
						widget.addSaveButton(form, saveFctn)
 | 
				
			||||||
 | 
						widget.addCancelButton(form)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) addCancelButton(form *tview.Form) {
 | 
				
			||||||
 | 
						form.AddButton("Cancel", func() {
 | 
				
			||||||
 | 
							widget.pages.RemovePage("modal")
 | 
				
			||||||
 | 
							widget.app.SetFocus(widget.View)
 | 
				
			||||||
 | 
							widget.display()
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) addSaveButton(form *tview.Form, fctn func()) {
 | 
				
			||||||
 | 
						form.AddButton("Save", fctn)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) modalFocus(form *tview.Form) {
 | 
				
			||||||
 | 
						frame := widget.modalFrame(form)
 | 
				
			||||||
 | 
						widget.pages.AddPage("modal", frame, false, true)
 | 
				
			||||||
 | 
						widget.app.SetFocus(frame)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) modalForm(lbl, text string) *tview.Form {
 | 
				
			||||||
 | 
						form := tview.NewForm().
 | 
				
			||||||
 | 
							SetButtonsAlign(tview.AlignCenter).
 | 
				
			||||||
 | 
							SetButtonTextColor(tview.Styles.PrimaryTextColor)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						form.AddInputField(lbl, text, 60, nil, nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return form
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (widget *Widget) modalFrame(form *tview.Form) *tview.Frame {
 | 
				
			||||||
 | 
						_, _, w, h := widget.View.GetInnerRect()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
 | 
				
			||||||
 | 
						frame.SetBorder(true)
 | 
				
			||||||
 | 
						frame.SetRect(w+20, h+2, 80, 7)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return frame
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user