refactor: extract the Jobs view state, track selection by job ID
Phase 10 of the whole-project review (findings 5.1 and 5.2), folded into the ROADMAP file-split item as that plan asks. 5.2 was a real defect. `selected` was an index into a snapshot of the jobs slice, and every path that changed the slice patched it by hand. The one path that could not — adopting a different jobs file, where the Service replaces the whole list and the view only hears about it through the refresh JobsLoaded triggers — left the details pane redrawing from an index that belonged to the previous list, describing whichever job now sat there (or clearing when the new list was shorter) while the list highlight stayed put. The selection is now a job ID; rows are derived from it at render time, and refresh ends by pointing the highlight at the selected job, so the two can no longer disagree. 5.1: newJobsView was one 330-line constructor whose dozen closures shared seven mutable locals. It is now a jobsView struct over a jobsViewState that owns the snapshot, the folder filter, and the selection — the invariant that used to be maintained by hand in five places lives in one place — split across jobs_view.go (construction, refresh, layout), jobs_view_state.go, jobs_view_list.go, and jobs_view_toolbar.go. The folder-option rebuild that appeared verbatim in three handlers is one method. Behaviour that changed beyond the fix: switching the folder filter keeps the current selection when the new filter still shows it, instead of always jumping to the folder's first job. Docs: ARCHITECTURE records the new file layout and the selection-by-ID contract; ROADMAP drops jobs_view.go from the over-guideline table and refreshes the other five numbers (finding 2.4); TESTS documents the new state test file and the adoption regression test. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+159
-329
@@ -1,8 +1,6 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitea.mixdep.ru/mix/gosentry/src/app"
|
||||
"gitea.mixdep.ru/mix/gosentry/src/domain"
|
||||
|
||||
@@ -22,353 +20,185 @@ const noFolder = "No folder"
|
||||
// view; this panel is a quick at-a-glance summary anchored below the output.
|
||||
const maxJobActivityRows = 3
|
||||
|
||||
// jobsView owns the Jobs tab: the widgets, the view-only preferences they draw
|
||||
// (list mode and the scheduler pause label), and the jobsViewState the widgets
|
||||
// read. It replaces a single constructor whose dozen closures shared seven
|
||||
// mutable locals — the state each handler touches is now named on the struct
|
||||
// rather than captured, and the invariants that used to be maintained by hand in
|
||||
// five places live on jobsViewState.
|
||||
type jobsView struct {
|
||||
w fyne.Window
|
||||
svc *app.Service
|
||||
state *jobsViewState
|
||||
dp *detailsPanel
|
||||
|
||||
list *widget.List
|
||||
folderSelect *widget.Select
|
||||
viewButton *widget.Button
|
||||
stopAllButton *widget.Button
|
||||
schedulerState *widget.Label
|
||||
|
||||
// listView and paused mirror Service-owned config so the widgets can be
|
||||
// relabelled without a round trip. Both are re-read from the Service on every
|
||||
// refresh; neither is a source of truth.
|
||||
listView domain.JobListView
|
||||
paused bool
|
||||
}
|
||||
|
||||
// newJobsView builds the Jobs tab: list sidebar, details panel, and toolbar.
|
||||
// It returns the assembled panel and a refresh function the caller invokes
|
||||
// whenever the service state may have changed (e.g., from the event subscriber
|
||||
// in mainwindow.go). The refresh function re-reads the service snapshot and
|
||||
// redraws all widgets in the jobs view; it does NOT touch history or settings.
|
||||
func newJobsView(w fyne.Window, svc *app.Service) (fyne.CanvasObject, func()) {
|
||||
jobs := svc.Jobs()
|
||||
runtimes := make(map[int]*domain.JobRuntime, len(jobs))
|
||||
syncFromService := func() {
|
||||
jobs = svc.Jobs()
|
||||
for id := range runtimes {
|
||||
delete(runtimes, id)
|
||||
}
|
||||
for _, current := range jobs {
|
||||
if rt := svc.Runtime(current.ID); rt != nil {
|
||||
runtimes[current.ID] = rt
|
||||
}
|
||||
}
|
||||
config := svc.Config()
|
||||
v := &jobsView{
|
||||
w: w,
|
||||
svc: svc,
|
||||
state: newJobsViewState(svc),
|
||||
listView: config.JobListView,
|
||||
paused: config.Paused,
|
||||
}
|
||||
syncFromService()
|
||||
runtimeFor := func(index int) *domain.JobRuntime {
|
||||
if index < 0 || index >= len(jobs) {
|
||||
return &domain.JobRuntime{}
|
||||
}
|
||||
if rt := runtimes[jobs[index].ID]; rt != nil {
|
||||
return rt
|
||||
}
|
||||
return &domain.JobRuntime{}
|
||||
v.dp = newDetailsPanel(job{}, &domain.JobRuntime{}, config.OverlapPolicy, config.DefaultTimeoutSeconds)
|
||||
v.updateDetails()
|
||||
|
||||
// Build order follows what refresh() touches: the folder select fires its
|
||||
// OnChanged from SetSelected below, which refreshes, so every widget that
|
||||
// refresh() reaches has to exist by then.
|
||||
v.list = v.newList()
|
||||
v.viewButton = v.newViewToggle()
|
||||
globalControls := v.newGlobalControls()
|
||||
v.folderSelect = v.newFolderSelect()
|
||||
v.folderSelect.SetSelected(v.state.folder)
|
||||
v.syncListSelection()
|
||||
|
||||
return v.assemble(globalControls), v.refresh
|
||||
}
|
||||
|
||||
// refresh re-reads the Service and redraws the whole view. It is the single
|
||||
// entry point for "something changed": the toolbar handlers call it after a
|
||||
// successful operation, and mainwindow's event observer calls it for everything
|
||||
// else.
|
||||
func (v *jobsView) refresh() {
|
||||
v.state.sync()
|
||||
// The pause state is Service-owned and can change from outside this view, so
|
||||
// it is re-read here rather than mirrored from the tap handler alone — that is
|
||||
// what makes this view a consumer of SchedulerStateChanged.
|
||||
v.applySchedulerState(v.svc.Config().Paused)
|
||||
v.updateDetails()
|
||||
v.dp.logs.Refresh()
|
||||
v.list.Refresh()
|
||||
v.syncListSelection()
|
||||
}
|
||||
|
||||
// updateDetails repopulates the details pane from the current selection.
|
||||
func (v *jobsView) updateDetails() {
|
||||
current, ok := v.state.selected()
|
||||
if !ok {
|
||||
// A folder filter can temporarily leave no selectable rows. Clearing the
|
||||
// details panel avoids showing stale information for a hidden job.
|
||||
v.dp.clear()
|
||||
return
|
||||
}
|
||||
// Overlap policy and the default timeout are global settings that can change
|
||||
// from the Settings tab while this view is open, so they are re-read on every
|
||||
// update rather than captured once at construction.
|
||||
config := v.svc.Config()
|
||||
v.dp.update(current, v.state.runtime(current.ID), config.OverlapPolicy, config.DefaultTimeoutSeconds)
|
||||
}
|
||||
|
||||
selected := 0
|
||||
if len(jobs) == 0 {
|
||||
selected = -1
|
||||
// syncListSelection points the list's highlight at the selected job. It is what
|
||||
// keeps the highlight and the details pane describing the same job when the row
|
||||
// a job sits in moves — a job created or deleted above it, a folder filter
|
||||
// applied, or a different jobs file adopted. widget.List.Select returns early
|
||||
// when the row is already highlighted, so calling this on every refresh does not
|
||||
// fight the user's scrolling.
|
||||
func (v *jobsView) syncListSelection() {
|
||||
row := v.state.displayRow()
|
||||
if row < 0 {
|
||||
v.list.UnselectAll()
|
||||
return
|
||||
}
|
||||
selectedFolder := allFolders
|
||||
initialConfig := svc.Config()
|
||||
schedulerPaused := initialConfig.Paused
|
||||
listView := initialConfig.JobListView
|
||||
filteredJobs := filteredJobIndexes(jobs, selectedFolder)
|
||||
v.list.Select(row)
|
||||
}
|
||||
|
||||
dp := newDetailsPanel(job{}, &domain.JobRuntime{}, initialConfig.OverlapPolicy, initialConfig.DefaultTimeoutSeconds)
|
||||
if selected >= 0 {
|
||||
dp.update(jobs[selected], runtimeFor(selected), initialConfig.OverlapPolicy, initialConfig.DefaultTimeoutSeconds)
|
||||
} else {
|
||||
dp.clear()
|
||||
}
|
||||
// rebuildFolders re-derives the folder filter's options from the current jobs.
|
||||
// Creating, editing, and deleting a job can all add or remove a folder.
|
||||
func (v *jobsView) rebuildFolders() {
|
||||
v.folderSelect.Options = folderOptions(v.state.jobs)
|
||||
v.folderSelect.Refresh()
|
||||
}
|
||||
|
||||
updateDetails := func(index int) {
|
||||
if index < 0 || index >= len(jobs) {
|
||||
// A folder filter can temporarily leave no selectable rows. Clearing
|
||||
// the details panel avoids showing stale information for a hidden job.
|
||||
dp.clear()
|
||||
return
|
||||
}
|
||||
selected = index
|
||||
// Overlap policy and the default timeout are global settings that can
|
||||
// change from the Settings tab while this view is open, so they are
|
||||
// re-read on every update rather than captured once at construction.
|
||||
config := svc.Config()
|
||||
dp.update(jobs[selected], runtimeFor(selected), config.OverlapPolicy, config.DefaultTimeoutSeconds)
|
||||
}
|
||||
|
||||
// list, folderSelect, and applySchedulerState are declared early so closures
|
||||
// below can reference them before the widgets that assign the values exist.
|
||||
var list *widget.List
|
||||
var folderSelect *widget.Select
|
||||
var applySchedulerState func(bool)
|
||||
|
||||
refreshView := func() {
|
||||
syncFromService()
|
||||
if applySchedulerState != nil {
|
||||
// The pause state is Service-owned and can change from outside this
|
||||
// view (Settings has no such control today, but the event that
|
||||
// reports it — SchedulerStateChanged — is consumed here rather than
|
||||
// relying solely on the tap handler's own mirror).
|
||||
applySchedulerState(svc.Config().Paused)
|
||||
}
|
||||
filteredJobs = filteredJobIndexes(jobs, selectedFolder)
|
||||
updateDetails(selected)
|
||||
dp.logs.Refresh()
|
||||
if list != nil {
|
||||
list.Refresh()
|
||||
}
|
||||
}
|
||||
|
||||
// applyRowMode expresses the current view mode as visibility on the row's
|
||||
// four labels. widget.List caches the row template's MinSize, and
|
||||
// list.Refresh() re-creates the template and recomputes it, so hiding lines
|
||||
// is what actually shrinks the rows: layout.NewCustomPaddedVBoxLayout and the
|
||||
// border layout both skip hidden children when measuring.
|
||||
applyRowMode := func(inlineStatus, meta, status fyne.CanvasObject) {
|
||||
if listView.IsCompact() {
|
||||
inlineStatus.Show()
|
||||
meta.Hide()
|
||||
status.Hide()
|
||||
return
|
||||
}
|
||||
inlineStatus.Hide()
|
||||
meta.Show()
|
||||
status.Show()
|
||||
}
|
||||
|
||||
list = widget.NewList(
|
||||
func() int { return len(filteredJobs) },
|
||||
func() fyne.CanvasObject {
|
||||
name := widget.NewLabelWithStyle("Job name", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
|
||||
// Truncating stops a long name from pushing the compact row's status
|
||||
// off the right-hand edge. Labels default to TextWrapOff, which grows
|
||||
// the widget to fit instead.
|
||||
name.Truncation = fyne.TextTruncateClip
|
||||
inlineStatus := widget.NewLabel("status")
|
||||
meta := widget.NewLabel("schedule")
|
||||
status := widget.NewLabel("status")
|
||||
applyRowMode(inlineStatus, meta, status)
|
||||
nameLine := container.NewBorder(nil, nil, nil, inlineStatus, name)
|
||||
return container.New(layout.NewCustomPaddedVBoxLayout(rowOverlap()), nameLine, meta, status)
|
||||
},
|
||||
func(id widget.ListItemID, item fyne.CanvasObject) {
|
||||
row := item.(*fyne.Container)
|
||||
// NewBorder keeps the center object first and appends the border slots
|
||||
// after it, so nameLine is [name, inlineStatus].
|
||||
nameLine := row.Objects[0].(*fyne.Container)
|
||||
name := nameLine.Objects[0].(*widget.Label)
|
||||
inlineStatus := nameLine.Objects[1].(*widget.Label)
|
||||
meta := row.Objects[1].(*widget.Label)
|
||||
status := row.Objects[2].(*widget.Label)
|
||||
|
||||
current := jobs[filteredJobs[id]]
|
||||
name.SetText(current.Name)
|
||||
// Keep each row compact: folder, schedule, and command are shown in one
|
||||
// metadata line so the left pane stays useful even with many jobs.
|
||||
meta.SetText(app.DisplayFolder(current.Folder) + " " + current.Schedule + " " + app.DisplayInvocation(current))
|
||||
statusText := app.StatusText(current, runtimes[current.ID])
|
||||
status.SetText(statusText)
|
||||
inlineStatus.SetText(statusText)
|
||||
// A full Refresh reuses rows built under the previous mode, so
|
||||
// visibility cannot be left to the create callback alone.
|
||||
applyRowMode(inlineStatus, meta, status)
|
||||
},
|
||||
)
|
||||
list.OnSelected = func(id widget.ListItemID) {
|
||||
if id < 0 || id >= len(filteredJobs) {
|
||||
updateDetails(-1)
|
||||
return
|
||||
}
|
||||
updateDetails(filteredJobs[id])
|
||||
}
|
||||
if len(filteredJobs) > 0 && selected >= 0 {
|
||||
list.Select(app.DisplayIndex(filteredJobs, selected))
|
||||
}
|
||||
|
||||
folderSelect = widget.NewSelect(folderOptions(jobs), func(value string) {
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
selectedFolder = value
|
||||
filteredJobs = filteredJobIndexes(jobs, selectedFolder)
|
||||
if len(filteredJobs) == 0 {
|
||||
// The "No folder" filter is intentionally allowed to be empty. It is a
|
||||
// real filter choice, not an error state, so the selection is cleared.
|
||||
// This path returns without reaching refreshView(), so it is the one
|
||||
// place the list has to be redrawn by hand.
|
||||
selected = -1
|
||||
updateDetails(-1)
|
||||
list.Refresh()
|
||||
return
|
||||
}
|
||||
selected = filteredJobs[0]
|
||||
list.Select(0)
|
||||
refreshView()
|
||||
})
|
||||
folderSelect.SetSelected(selectedFolder)
|
||||
|
||||
// viewToggleIcon pairs with viewToggleText: both name the action the button
|
||||
// performs, not the state it is in, matching stopAllButton's convention.
|
||||
viewToggleIcon := func(current domain.JobListView) fyne.Resource {
|
||||
if current.IsCompact() {
|
||||
return theme.ViewFullScreenIcon()
|
||||
}
|
||||
return theme.ListIcon()
|
||||
}
|
||||
viewButton := widget.NewButtonWithIcon(viewToggleText(listView), viewToggleIcon(listView), nil)
|
||||
viewButton.OnTapped = func() {
|
||||
next := nextJobListView(listView)
|
||||
listView = next
|
||||
if err := svc.SetJobListView(next); err != nil {
|
||||
// Roll the mode back and leave the button as it was, so the button
|
||||
// never claims a preference that did not reach disk.
|
||||
listView = nextJobListView(next)
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
viewButton.SetText(viewToggleText(listView))
|
||||
viewButton.SetIcon(viewToggleIcon(listView))
|
||||
// Refresh re-creates the row template, which is what recomputes the
|
||||
// cached row height for the new mode. Selection is untouched.
|
||||
list.Refresh()
|
||||
}
|
||||
|
||||
addButton := widget.NewButtonWithIcon("New job", theme.ContentAddIcon(), func() {
|
||||
showJobDialog(w, "New job", job{Schedule: "@every 1m", Command: "echo GoSentry job ran", Enabled: true}, func(saved job) {
|
||||
created, err := svc.CreateJob(saved)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
syncFromService()
|
||||
folderSelect.Options = folderOptions(jobs)
|
||||
folderSelect.Refresh()
|
||||
targetFolder := filterValue(created.Folder)
|
||||
if selectedFolder != allFolders && selectedFolder != targetFolder {
|
||||
selectedFolder = targetFolder
|
||||
folderSelect.SetSelected(targetFolder)
|
||||
}
|
||||
selected = indexOfID(jobs, created.ID)
|
||||
filteredJobs = filteredJobIndexes(jobs, selectedFolder)
|
||||
list.Select(app.DisplayIndex(filteredJobs, selected))
|
||||
refreshView()
|
||||
})
|
||||
})
|
||||
editButton := widget.NewButtonWithIcon("Edit", theme.DocumentCreateIcon(), func() {
|
||||
if selected < 0 || selected >= len(jobs) {
|
||||
return
|
||||
}
|
||||
showJobDialog(w, "Edit job", jobs[selected], func(saved job) {
|
||||
saved.ID = jobs[selected].ID
|
||||
if err := svc.UpdateJob(saved); err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
syncFromService()
|
||||
folderSelect.Options = folderOptions(jobs)
|
||||
folderSelect.Refresh()
|
||||
refreshView()
|
||||
})
|
||||
})
|
||||
runButton := widget.NewButtonWithIcon("Run now", theme.MediaPlayIcon(), func() {
|
||||
if selected < 0 || selected >= len(jobs) {
|
||||
return
|
||||
}
|
||||
// A manual run is allowed even while the scheduler is paused: pause only
|
||||
// stops automatic scheduled runs, not the user's explicit "Run now".
|
||||
if err := svc.RunNow(jobs[selected].ID); err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
refreshView()
|
||||
})
|
||||
|
||||
schedulerState := widget.NewLabel("")
|
||||
stopAllButton := widget.NewButtonWithIcon("", nil, nil)
|
||||
// applySchedulerState is the one place that draws the pause control and its
|
||||
// status text from a pause value, so refreshView can drive it from whatever
|
||||
// the Service reports (including a SchedulerStateChanged the general refresh
|
||||
// picks up) instead of only the tap handler mirroring its own toggle.
|
||||
applySchedulerState = func(paused bool) {
|
||||
schedulerPaused = paused
|
||||
if paused {
|
||||
schedulerState.SetText("Scheduler paused")
|
||||
stopAllButton.SetText("Enable auto")
|
||||
stopAllButton.SetIcon(theme.MediaPlayIcon())
|
||||
} else {
|
||||
schedulerState.SetText("Scheduler running")
|
||||
stopAllButton.SetText("Disable auto")
|
||||
stopAllButton.SetIcon(theme.MediaPauseIcon())
|
||||
}
|
||||
}
|
||||
applySchedulerState(schedulerPaused)
|
||||
stopAllButton.OnTapped = func() {
|
||||
// SetGlobalPause flips the pause flag, updates every job's next-run text,
|
||||
// and emits the activity record the observer logs. Revert if the save fails.
|
||||
if err := svc.SetGlobalPause(!schedulerPaused); err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
// refreshView re-derives the pause state from the Service (see
|
||||
// applySchedulerState above), so it is the single place that draws it.
|
||||
refreshView()
|
||||
}
|
||||
pauseButton := widget.NewButtonWithIcon("Pause", theme.MediaPauseIcon(), func() {
|
||||
if selected < 0 || selected >= len(jobs) {
|
||||
return
|
||||
}
|
||||
current := jobs[selected]
|
||||
if err := svc.SetEnabled(current.ID, !current.Enabled); err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
refreshView()
|
||||
})
|
||||
deleteButton := widget.NewButtonWithIcon("Delete", theme.DeleteIcon(), func() {
|
||||
if selected < 0 || selected >= len(jobs) {
|
||||
return
|
||||
}
|
||||
deleted := jobs[selected]
|
||||
// Deletion is confirmed because jobs can represent real system actions.
|
||||
// There is no undo yet, so accidental removal should require one more click.
|
||||
dialog.ShowConfirm("Delete job", fmt.Sprintf("Delete %q?", deleted.Name), func(confirm bool) {
|
||||
if !confirm {
|
||||
return
|
||||
}
|
||||
if err := svc.DeleteJob(deleted.ID); err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
syncFromService()
|
||||
folderSelect.Options = folderOptions(jobs)
|
||||
folderSelect.Refresh()
|
||||
filteredJobs = filteredJobIndexes(jobs, selectedFolder)
|
||||
if len(filteredJobs) == 0 && selectedFolder != allFolders {
|
||||
selectedFolder = allFolders
|
||||
folderSelect.SetSelected(allFolders)
|
||||
filteredJobs = filteredJobIndexes(jobs, selectedFolder)
|
||||
}
|
||||
if len(filteredJobs) == 0 {
|
||||
selected = -1
|
||||
} else {
|
||||
selected = filteredJobs[0]
|
||||
}
|
||||
if selected >= 0 {
|
||||
list.Select(app.DisplayIndex(filteredJobs, selected))
|
||||
}
|
||||
refreshView()
|
||||
}, w)
|
||||
})
|
||||
|
||||
toolbar := container.NewHBox(addButton, editButton, runButton, pauseButton, deleteButton, layout.NewSpacer())
|
||||
// The row sits directly under the tab bar with no AppTabs inset, while the
|
||||
// default VBox gap below it is one theme padding — add the same on top so
|
||||
// the button is not flush against the tabs.
|
||||
globalControls := container.New(
|
||||
layout.NewCustomPaddedLayout(theme.Padding(), 0, 0, 0),
|
||||
container.NewHBox(stopAllButton, schedulerState, layout.NewSpacer()),
|
||||
)
|
||||
// assemble puts the sidebar (global controls, folder filter, toolbar, list) and
|
||||
// the details pane into the master/detail split the tab shows.
|
||||
func (v *jobsView) assemble(globalControls fyne.CanvasObject) fyne.CanvasObject {
|
||||
// The whole filter is one row: caption on the left, view toggle on the right,
|
||||
// select filling what is left. The border layout gives both edges their
|
||||
// MinSize, so the header is a line shorter than a stacked caption would make it.
|
||||
folderCaption := widget.NewLabelWithStyle("Folder", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
|
||||
filterRow := container.NewBorder(nil, nil, folderCaption, viewButton, folderSelect)
|
||||
sidebarHeader := container.NewVBox(globalControls, widget.NewSeparator(), filterRow, toolbar)
|
||||
sidebar := container.NewBorder(sidebarHeader, nil, nil, nil, list)
|
||||
filterRow := container.NewBorder(nil, nil, folderCaption, v.viewButton, v.folderSelect)
|
||||
sidebarHeader := container.NewVBox(globalControls, widget.NewSeparator(), filterRow, v.newToolbar())
|
||||
sidebar := container.NewBorder(sidebarHeader, nil, nil, nil, v.list)
|
||||
|
||||
// A split rather than a Border left slot: the border pinned the sidebar at its
|
||||
// MinSize forever, so the user could never trade list width for detail width.
|
||||
// The divider lets either pane grow, and neither can be dragged below its own
|
||||
// content minimum.
|
||||
panel := container.NewHSplit(sidebar, container.NewPadded(dp.container()))
|
||||
panel := container.NewHSplit(sidebar, container.NewPadded(v.dp.container()))
|
||||
panel.SetOffset(initialSplitOffset(sidebar.MinSize().Width))
|
||||
return panel, refreshView
|
||||
return panel
|
||||
}
|
||||
|
||||
// newFolderSelect builds the folder filter. Selecting a folder narrows the list
|
||||
// and, when the selected job is no longer visible, moves the selection to the
|
||||
// first row that is (see jobsViewState.applyFilter).
|
||||
func (v *jobsView) newFolderSelect() *widget.Select {
|
||||
return widget.NewSelect(folderOptions(v.state.jobs), func(value string) {
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
v.state.applyFilter(value)
|
||||
v.refresh()
|
||||
})
|
||||
}
|
||||
|
||||
// newGlobalControls builds the pause control row that sits above the filter.
|
||||
func (v *jobsView) newGlobalControls() fyne.CanvasObject {
|
||||
v.schedulerState = widget.NewLabel("")
|
||||
v.stopAllButton = widget.NewButtonWithIcon("", nil, nil)
|
||||
v.applySchedulerState(v.paused)
|
||||
v.stopAllButton.OnTapped = func() {
|
||||
// SetGlobalPause flips the pause flag, updates every job's next-run text,
|
||||
// and emits the activity record the observer logs. refresh re-derives the
|
||||
// pause state from the Service, so a failed save leaves the control showing
|
||||
// what actually happened.
|
||||
if err := v.svc.SetGlobalPause(!v.paused); err != nil {
|
||||
dialog.ShowError(err, v.w)
|
||||
return
|
||||
}
|
||||
v.refresh()
|
||||
}
|
||||
// The row sits directly under the tab bar with no AppTabs inset, while the
|
||||
// default VBox gap below it is one theme padding — add the same on top so
|
||||
// the button is not flush against the tabs.
|
||||
return container.New(
|
||||
layout.NewCustomPaddedLayout(theme.Padding(), 0, 0, 0),
|
||||
container.NewHBox(v.stopAllButton, v.schedulerState, layout.NewSpacer()),
|
||||
)
|
||||
}
|
||||
|
||||
// applySchedulerState is the one place that draws the pause control and its
|
||||
// status text from a pause value, so refresh can drive it from whatever the
|
||||
// Service reports instead of only the tap handler mirroring its own toggle.
|
||||
func (v *jobsView) applySchedulerState(paused bool) {
|
||||
v.paused = paused
|
||||
if paused {
|
||||
v.schedulerState.SetText("Scheduler paused")
|
||||
v.stopAllButton.SetText("Enable auto")
|
||||
v.stopAllButton.SetIcon(theme.MediaPlayIcon())
|
||||
return
|
||||
}
|
||||
v.schedulerState.SetText("Scheduler running")
|
||||
v.stopAllButton.SetText("Disable auto")
|
||||
v.stopAllButton.SetIcon(theme.MediaPauseIcon())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user