Skip to content

Commit

Permalink
Readd OnSelected callback to Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
PucklaJ committed Dec 14, 2023
1 parent 01a67ed commit 9bd1fe0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
20 changes: 13 additions & 7 deletions cmd/calendar_demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ 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.onChanged)
calendar := xwidget.NewCalendarWithMode(startingDate, d.onChanged, xwidget.CalendarSingle)
calendar.OnSelected = d.onSelected

selection := widget.NewRadioGroup([]string{"Single", "Multi", "Range"}, func(s string) {
calendar.ClearSelection()
Expand All @@ -47,6 +49,7 @@ func main() {
c := container.NewVBox(
i,
scroll,
ll,
calendar,
selection,
)
Expand All @@ -56,16 +59,19 @@ func main() {
}

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

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

func (d *date) onSelected(selectedDate time.Time) {
d.dateSelected.SetText("OnSelected: " + selectedDate.Format("Mon 02 Jan 2006"))
}
28 changes: 26 additions & 2 deletions widget/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type Calendar struct {
SelectionMode int
SelectedDates []time.Time
OnChanged func([]time.Time)
OnSelected func(time.Time)
}

func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
Expand Down Expand Up @@ -145,10 +146,10 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
b = widget.NewButton(s, func() {

selectedDate := c.dateForButton(dayNum)
isSelected := false

if c.SelectionMode == CalendarSingle {
// Unselect all currently selected buttons
isSelected := false
for _, t := range c.SelectedDates {
tYear, tMonth, tDay := t.Date()

Expand All @@ -169,6 +170,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
} else {
c.SelectedDates = c.SelectedDates[:0]
}
isSelected = !isSelected
} else if c.SelectionMode == CalendarMulti {
// Only add it to the slice if it does not contain it
var index int
Expand All @@ -184,6 +186,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
if !found {
c.SelectedDates = append(c.SelectedDates, selectedDate)
b.Importance = calendarSelectColor
isSelected = true
} else {
c.SelectedDates = append(c.SelectedDates[:index], c.SelectedDates[index+1:]...)
b.Importance = calendarUnselectColor
Expand All @@ -200,6 +203,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
dateButtons[sDay].Refresh()

c.SelectedDates = append(c.SelectedDates, selectedDate)
isSelected = true
} else if len(c.SelectedDates) == 1 {
if dateEquals(c.SelectedDates[0], selectedDate) {
sDay := selectedDate.Day()
Expand Down Expand Up @@ -238,6 +242,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
}

c.SelectedDates = allDates
isSelected = true
}
} else {
if dateEquals(c.SelectedDates[0], selectedDate) {
Expand Down Expand Up @@ -293,6 +298,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
}

c.SelectedDates = c.SelectedDates[index:]
isSelected = true
} else {
// Change the end to the current one
for _, t := range c.SelectedDates[index+1:] {
Expand All @@ -304,6 +310,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
}

c.SelectedDates = c.SelectedDates[:index+1]
isSelected = true
}
} else {
// if a date outside start and end is clicked
Expand Down Expand Up @@ -343,13 +350,18 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {

c.SelectedDates = append(c.SelectedDates, selectedDate)
}

isSelected = true
}
}
}

if c.OnChanged != nil {
c.OnChanged(c.SelectedDates)
}
if isSelected && c.OnSelected != nil {
c.OnSelected(selectedDate)
}
})

// Give the selected dates a different importance
Expand Down Expand Up @@ -432,10 +444,22 @@ func (c *Calendar) CreateRenderer() fyne.WidgetRenderer {
}

// NewCalendar creates a calendar instance
func NewCalendar(cT time.Time, onChanged func([]time.Time)) *Calendar {
func NewCalendar(cT time.Time, onSelected func(time.Time)) *Calendar {
c := &Calendar{
currentTime: cT,
SelectionMode: CalendarSingle,
OnSelected: onSelected,
}

c.ExtendBaseWidget(c)

return c
}

func NewCalendarWithMode(cT time.Time, onChanged func([]time.Time), mode int) *Calendar {
c := &Calendar{
currentTime: cT,
SelectionMode: mode,
OnChanged: onChanged,
}

Expand Down

0 comments on commit 9bd1fe0

Please sign in to comment.