/
settings.go
107 lines (86 loc) · 2.16 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2013 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"math/rand"
"strings"
"time"
)
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
func main() {
app := walk.App()
// These specify the app data sub directory for the settings file.
app.SetOrganizationName("The Walk Authors")
app.SetProductName("Walk Settings Example")
// Settings file name.
settings := walk.NewIniFileSettings("settings.ini")
// All settings marked as expiring will expire after this duration w/o use.
// This applies to all widgets settings.
settings.SetExpireDuration(time.Hour * 24 * 30 * 3)
if err := settings.Load(); err != nil {
log.Fatal(err)
}
app.SetSettings(settings)
if err := RunMainWindow(); err != nil {
log.Fatal(err)
}
if err := settings.Save(); err != nil {
log.Fatal(err)
}
}
func RunMainWindow() error {
if _, err := (MainWindow{
Name: "mainWindow", // Name is needed for settings persistence
Title: "Walk Settings Example",
MinSize: Size{800, 600},
Layout: VBox{},
Children: []Widget{
TableView{
Name: "tableView", // Name is needed for settings persistence
AlternatingRowBGColor: walk.RGB(255, 255, 200),
ColumnsOrderable: true,
Columns: []TableViewColumn{
{DataMember: "Index"},
{DataMember: "Bar"},
{DataMember: "Baz", Format: "%.2f", Alignment: AlignFar},
{DataMember: "Quux", Format: "2006-01-02 15:04:05", Width: 150},
},
Model: NewFooModel(),
}},
}.Run()); err != nil {
return err
}
return nil
}
func NewFooModel() *FooModel {
now := time.Now()
rand.Seed(now.UnixNano())
m := &FooModel{items: make([]*Foo, 1000)}
for i := range m.items {
m.items[i] = &Foo{
Index: i,
Bar: strings.Repeat("*", rand.Intn(5)+1),
Baz: rand.Float64() * 1000,
Quux: time.Unix(rand.Int63n(now.Unix()), 0),
}
}
return m
}
type FooModel struct {
walk.SortedReflectTableModelBase
items []*Foo
}
func (m *FooModel) Items() interface{} {
return m.items
}
type Foo struct {
Index int
Bar string
Baz float64
Quux time.Time
}