-
Notifications
You must be signed in to change notification settings - Fork 2
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
Calendar picker. #10 #26
Changes from 4 commits
866356d
c9ad0a4
65aff20
c3c1fd4
95989bc
c060c24
29d3251
f281b2c
a6aa124
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
|
||
"fyne.io/fyne/v2" | ||
) | ||
|
||
// Declare conformity with Layout interface | ||
var ( | ||
_ fyne.Layout = (*calendarLayout)(nil) | ||
padding float32 = 0 | ||
cellSize float64 = 32 | ||
) | ||
|
||
type calendarLayout struct { | ||
Cols int | ||
vertical, adapt bool | ||
} | ||
|
||
func NewCalendarLayout(s float64) fyne.Layout { | ||
cellSize = s | ||
return &calendarLayout{Cols: 7} | ||
} | ||
|
||
func (g *calendarLayout) horizontal() bool { | ||
if g.adapt { | ||
return fyne.IsHorizontal(fyne.CurrentDevice().Orientation()) | ||
} | ||
|
||
return !g.vertical | ||
} | ||
|
||
func (g *calendarLayout) countRows(objects []fyne.CanvasObject) int { | ||
count := 0 | ||
for _, child := range objects { | ||
if child.Visible() { | ||
count++ | ||
} | ||
} | ||
|
||
return int(math.Ceil(float64(count) / float64(g.Cols))) | ||
} | ||
|
||
// Get the leading (top or left) edge of a grid cell. | ||
// size is the ideal cell size and the offset is which col or row its on. | ||
func getLeading(size float64, offset int) float32 { | ||
ret := (size + float64(padding)) * float64(offset) | ||
|
||
return float32(math.Round(ret)) | ||
} | ||
|
||
// Get the trailing (bottom or right) edge of a grid cell. | ||
// size is the ideal cell size and the offset is which col or row its on. | ||
func getTrailing(size float64, offset int) float32 { | ||
return getLeading(size, offset+1) - padding | ||
} | ||
|
||
// Layout is called to pack all child objects into a specified size. | ||
// For a GridLayout this will pack objects into a table format with the number | ||
// of columns specified in our constructor. | ||
func (g *calendarLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) { | ||
row, col := 0, 0 | ||
i := 0 | ||
for _, child := range objects { | ||
if !child.Visible() { | ||
continue | ||
} | ||
|
||
x1 := getLeading(cellSize, col) | ||
y1 := getLeading(cellSize, row) | ||
x2 := getTrailing(cellSize, col) | ||
y2 := getTrailing(cellSize, row) | ||
|
||
child.Move(fyne.NewPos(x1, y1)) | ||
child.Resize(fyne.NewSize(x2-x1, y2-y1)) | ||
|
||
if g.horizontal() { | ||
if (i+1)%g.Cols == 0 { | ||
row++ | ||
col = 0 | ||
} else { | ||
col++ | ||
} | ||
} else { | ||
if (i+1)%g.Cols == 0 { | ||
col++ | ||
row = 0 | ||
} else { | ||
row++ | ||
} | ||
} | ||
i++ | ||
} | ||
} | ||
|
||
func (g *calendarLayout) MinSize(objects []fyne.CanvasObject) fyne.Size { | ||
rows := g.countRows(objects) | ||
return fyne.NewSize(float32(cellSize+float64(padding))*7, float32(cellSize+float64(padding))*float32(rows)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ package main | |
import ( | ||
"fmt" | ||
"image/color" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/canvas" | ||
|
@@ -22,18 +24,126 @@ type location struct { | |
location *city | ||
session *unsplashSession | ||
|
||
date *widget.Select | ||
time *widget.SelectEntry | ||
button *widget.Button | ||
dots *fyne.Container | ||
|
||
selectedDay int | ||
selectedMonth int | ||
selectedYear int | ||
|
||
dateButton *widget.Button | ||
monthLabel *widget.RichText | ||
monthPrevious *widget.Button | ||
monthNext *widget.Button | ||
|
||
dateContainer *fyne.Container | ||
calendar *fyne.Container | ||
} | ||
|
||
func daysOfMonth(l *location) []fyne.CanvasObject { | ||
start, _ := time.Parse("2006-1-2", strconv.Itoa(l.selectedYear)+"-"+strconv.Itoa(l.selectedMonth)+"-"+strconv.Itoa(1)) | ||
|
||
buttons := []fyne.CanvasObject{} | ||
|
||
//account for Go time pkg starting on sunday at index 0 | ||
dayIndex := int(start.Weekday()) | ||
if dayIndex == 0 { | ||
dayIndex += 7 | ||
} | ||
|
||
//add spacers if week doesn't start on Monday | ||
for i := 0; i < dayIndex-1; i++ { | ||
buttons = append(buttons, layout.NewSpacer()) | ||
} | ||
|
||
for d := start; d.Month() == start.Month(); d = d.AddDate(0, 0, 1) { | ||
|
||
s := fmt.Sprint(d.Day()) | ||
var b fyne.CanvasObject = widget.NewButton(s, func() { | ||
//functionality for task #12 "Change time using calendar and time picker affecting all city" | ||
//to go here | ||
fmt.Println("Date selected = "+s, d.Month(), d.Year()) | ||
}) | ||
|
||
buttons = append(buttons, b) | ||
} | ||
|
||
return buttons | ||
} | ||
|
||
func monthYear(l *location) string { | ||
return time.Month(l.selectedMonth).String() + " " + strconv.Itoa(l.selectedYear) | ||
} | ||
|
||
func dayMonthYear(l *location) string { | ||
d, _ := time.Parse("2006-1-2", strconv.Itoa(l.selectedYear)+"-"+strconv.Itoa(l.selectedMonth)+"-"+strconv.Itoa(l.selectedDay)) | ||
return d.Weekday().String()[:3] + " " + d.Month().String() + " " + strconv.Itoa(d.Year()) | ||
} | ||
|
||
func columnHeadings(textSize float32) []fyne.CanvasObject { | ||
l := []fyne.CanvasObject{} | ||
for i := 0; i < 7; i++ { | ||
j := i + 1 | ||
if j == 7 { | ||
j = 0 | ||
} | ||
|
||
var canvasObject fyne.CanvasObject = canvas.NewText(strings.ToUpper(time.Weekday(j).String()[:3]), color.NRGBA{0xFF, 0xFF, 0xFF, 0xBF}) | ||
canvasObject.(*canvas.Text).TextSize = textSize | ||
canvasObject.(*canvas.Text).Alignment = fyne.TextAlignCenter | ||
l = append(l, canvasObject) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func calendarObjects(l *location) []fyne.CanvasObject { | ||
c := columnHeadings(8) | ||
c = append(c, daysOfMonth(l)...) | ||
|
||
return c | ||
} | ||
|
||
func navigation(l *location) { | ||
|
||
l.monthPrevious = widget.NewButtonWithIcon("", theme.NavigateBackIcon(), func() { | ||
l.selectedMonth-- | ||
if l.selectedMonth < 1 { | ||
l.selectedMonth = 12 | ||
l.selectedYear-- | ||
} | ||
l.monthLabel.ParseMarkdown(monthYear(l)) | ||
|
||
l.calendar.Objects = calendarObjects(l) | ||
}) | ||
l.monthNext = widget.NewButtonWithIcon("", theme.NavigateNextIcon(), func() { | ||
l.selectedMonth++ | ||
if l.selectedMonth > 12 { | ||
l.selectedMonth = 1 | ||
l.selectedYear++ | ||
} | ||
l.monthLabel.ParseMarkdown(monthYear(l)) | ||
|
||
l.calendar.Objects = calendarObjects(l) | ||
}) | ||
|
||
l.monthLabel = widget.NewRichTextFromMarkdown(monthYear(l)) | ||
} | ||
|
||
func calendar(l *location) { | ||
l.calendar = container.New(NewCalendarLayout(32), calendarObjects(l)...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to move all the function and logic related to the Calendar to its own file? I would also expect a l.calendar := newCalendar(). |
||
|
||
b := container.New(layout.NewBorderLayout(nil, nil, l.monthPrevious, l.monthNext), | ||
l.monthPrevious, l.monthNext, container.NewCenter(l.monthLabel)) | ||
|
||
l.dateContainer = container.NewVBox(b, l.calendar) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also an newCalendarPopup() with showAtPosition function. |
||
} | ||
|
||
func newLocation(loc *city, session *unsplashSession) *location { | ||
func newLocation(loc *city, session *unsplashSession, n *nomad) *location { | ||
l := &location{location: loc, session: session} | ||
l.ExtendBaseWidget(l) | ||
|
||
l.date = widget.NewSelect([]string{}, func(string) {}) | ||
l.date.PlaceHolder = loc.localTime.Format("Mon 02 Jan") | ||
l.time = widget.NewSelectEntry(listTimes()) | ||
l.time.PlaceHolder = "22:00" // longest | ||
l.time.Wrapping = fyne.TextWrapOff | ||
|
@@ -53,19 +163,29 @@ func newLocation(loc *city, session *unsplashSession) *location { | |
|
||
l.dots = container.NewVBox(layout.NewSpacer(), l.button) | ||
|
||
l.selectedDay = time.Now().Day() | ||
l.selectedMonth = int(time.Now().Month()) | ||
l.selectedYear = time.Now().Year() | ||
|
||
navigation(l) | ||
calendar(l) | ||
|
||
l.dateButton = widget.NewButton(dayMonthYear(l), func() { | ||
widget.ShowPopUpAtPosition(l.dateContainer, n.main.Canvas(), fyne.NewPos(0, l.Size().Height)) | ||
}) | ||
|
||
return l | ||
} | ||
|
||
func (l *location) CreateRenderer() fyne.WidgetRenderer { | ||
bg := canvas.NewImageFromResource(theme.FileImageIcon()) | ||
bg.Translucency = 0.5 | ||
city := widget.NewRichTextFromMarkdown("# " + strings.ToUpper(l.location.name)) | ||
|
||
city := widget.NewRichTextFromMarkdown("# " + l.location.name) | ||
location := canvas.NewText(" "+strings.ToUpper(l.location.country)+" · "+l.location.localTime.Format("MST"), locationTextColor) | ||
location.TextStyle.Monospace = true | ||
location.TextSize = 10 | ||
location.Move(fyne.NewPos(theme.Padding(), city.MinSize().Height-location.TextSize*.5)) | ||
input := container.NewBorder(nil, nil, l.date, l.time) | ||
input := container.NewBorder(nil, nil, l.dateButton, l.time) | ||
|
||
c := container.NewMax(bg, | ||
container.NewBorder(nil, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that you actually need to store this here.