Files

80 lines
2.5 KiB
Go

// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
package preferences
/*
#include <stdlib.h>
#include "native.h"
*/
import "C"
import (
"errors"
"time"
"unsafe"
"git.stormux.org/storm/cthulhu/internal/datetime"
"git.stormux.org/storm/cthulhu/internal/input"
)
func setFormatCombo(name string, options []datetime.Option, value string) {
id := C.CString(name)
defer C.free(unsafe.Pointer(id))
C.prefs_combo_clear(id)
for _, option := range options {
text := C.CString(option.Label)
C.prefs_combo_append(id, text)
C.free(unsafe.Pointer(text))
}
C.prefs_combo_set(id, C.int(formatIndex(options, value)))
}
func formatIndex(options []datetime.Option, value string) int {
for index, option := range options {
if option.Value == value {
return index
}
}
return 0
}
func (session *uiSession) set_general_values(values Values) {
session.progressBar = values.ProgressBar
session.setKeyBindings(values)
setCombo("progressBarOutputCombo", map[string]int{"off": 0, "beep": 1, "speak": 2, "both": 3}[values.ProgressBar.OutputMode])
setCheck("reportBackgroundProgressBarsCheckButton", values.ProgressBar.ReportBackground)
now := time.Now()
setFormatCombo("timeFormatCombo", datetime.TimeOptions(now), values.TimeFormat)
setFormatCombo("dateFormatCombo", datetime.DateOptions(now), values.DateFormat)
}
func (session *uiSession) get_general_values(values *Values) error {
values.KeyBindings = input.CloneBindingOverrides(session.bindingDraft)
values.KeyboardLayout = 1
if getCheck("generalLaptopButton") {
values.KeyboardLayout = 2
}
values.CthulhuModifierKeys = getModifierKeys()
ti, di := getCombo("timeFormatCombo"), getCombo("dateFormatCombo")
now := time.Now()
timeOptions := datetime.TimeOptions(now)
dateOptions := datetime.DateOptions(now)
if ti < 0 || ti >= len(timeOptions) || di < 0 || di >= len(dateOptions) {
return errors.New("Choose a time and date format.")
}
mode := getCombo("progressBarOutputCombo")
if mode < 0 || mode > 3 {
return errors.New("Choose a progress bar output mode.")
}
// Percentage intervals are configured in TOML, so retain them when the
// visible mode and background controls are applied or previewed.
values.ProgressBar = session.progressBar
values.ProgressBar.OutputMode = []string{"off", "beep", "speak", "both"}[mode]
values.ProgressBar.ReportBackground = getCheck("reportBackgroundProgressBarsCheckButton")
values.TimeFormat = timeOptions[ti].Value
values.DateFormat = dateOptions[di].Value
return nil
}