Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add different selection modes to Calendar (Single, Multi, Range) #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions cmd/calendar_demo/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"time"

"fyne.io/fyne/v2"
Expand All @@ -14,29 +15,63 @@ func main() {
a := app.New()
w := a.NewWindow("Calendar")

i := widget.NewLabel("Please Choose a Date")
i := widget.NewLabel("OnChanged")
i.Alignment = fyne.TextAlignCenter
l := widget.NewLabel("")
l.Alignment = fyne.TextAlignCenter
d := &date{instruction: i, dateChosen: l}
ll := widget.NewLabel("OnSelected: ")
d := &date{selectedDates: l, dateSelected: ll}

// Defines which date you would like the calendar to start
startingDate := time.Now()
calendar := xwidget.NewCalendar(startingDate, d.onSelected)
calendar := xwidget.NewCalendarWithMode(startingDate, d.onChanged, xwidget.CalendarSingle)
calendar.OnSelected = d.onSelected

c := container.NewVBox(i, l, calendar)
selection := widget.NewRadioGroup([]string{"Single", "Multi", "Range"}, func(s string) {
calendar.ClearSelection()
switch s {
case "Single":
calendar.SelectionMode = xwidget.CalendarSingle
case "Multi":
calendar.SelectionMode = xwidget.CalendarMulti
case "Range":
calendar.SelectionMode = xwidget.CalendarRange
}
calendar.Refresh()
})
selection.Horizontal = true
selection.Required = true
selection.Selected = "Single"

scroll := container.NewVScroll(l)
scroll.SetMinSize(fyne.NewSize(200, 100))

c := container.NewVBox(
i,
scroll,
ll,
calendar,
selection,
)

w.SetContent(c)
w.ShowAndRun()
}

type date struct {
instruction *widget.Label
dateChosen *widget.Label
selectedDates *widget.Label
dateSelected *widget.Label
}

func (d *date) onSelected(t time.Time) {
func (d *date) onChanged(selectedDates []time.Time) {
// use time object to set text on label with given format
d.instruction.SetText("Date Selected:")
d.dateChosen.SetText(t.Format("Mon 02 Jan 2006"))
var str string
for _, d := range selectedDates {
str += fmt.Sprint(d.Format("Mon 02 Jan 2006"), "\n")
}
d.selectedDates.SetText(str)
}

func (d *date) onSelected(selectedDate time.Time) {
d.dateSelected.SetText("OnSelected: " + selectedDate.Format("Mon 02 Jan 2006"))
}
Loading