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 4ab0187 commit 3b26095
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 @@ -99,6 +99,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 @@ -129,10 +130,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 @@ -153,6 +154,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 @@ -168,6 +170,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 @@ -184,6 +187,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 @@ -222,6 +226,7 @@ func (c *Calendar) daysOfMonth() []fyne.CanvasObject {
}

c.SelectedDates = allDates
isSelected = true
}
} else {
if dateEquals(c.SelectedDates[0], selectedDate) {
Expand Down Expand Up @@ -277,6 +282,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 @@ -288,6 +294,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 @@ -327,13 +334,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 @@ -416,10 +428,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 3b26095

Please sign in to comment.