-
Notifications
You must be signed in to change notification settings - Fork 8
/
settings.go
98 lines (82 loc) · 2.33 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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import "github.com/limetext/backend"
type (
// ToggleSetting Command toggles the value of a setting,
// making it false when it was true or true when it was false.
ToggleSetting struct {
backend.BypassUndoCommand
Setting string
}
// SetSetting Command set the value of a setting.
SetSetting struct {
backend.BypassUndoCommand
Setting string
Value interface{}
}
// ToggleSideBar Command enables us to toggle the sidebar
// when the sidebar is visible, it'll be made invisible
// and vice versa.
ToggleSideBar struct {
toggleSetting
}
// ToggleStatusBar command enables us to toggle the status bar.
ToggleStatusBar struct {
toggleSetting
}
// ToggleFullScreen command enables us to toggle full screen.
ToggleFullScreen struct {
toggleSetting
}
ToggleDistractionFree struct {
toggleSetting
}
ToggleMinimap struct {
toggleSetting
}
ToggleTabs struct {
toggleSetting
}
)
// helper struct for commands that just toggle a simple setting.
type toggleSetting struct {
// the setting name which we are going to toggle.
name string
backend.BypassUndoCommand
}
// Run executes the ToggleSetting command.
func (c *ToggleSetting) Run(v *backend.View, e *backend.Edit) error {
setting := c.Setting
prev, boolean := v.Settings().Get(setting, false).(bool)
// if the setting was non-boolean, it is set to true, else it is toggled
v.Settings().Set(setting, !boolean || !prev)
return nil
}
// Run executes the SetSetting command.
func (c *SetSetting) Run(v *backend.View, e *backend.Edit) error {
setting := c.Setting
v.Settings().Set(setting, c.Value)
return nil
}
func (t *toggleSetting) Run(w *backend.Window) error {
res, ok := w.Settings().Get(t.name, false).(bool)
w.Settings().Set(t.name, !ok || !res)
return nil
}
func toggle(name string) toggleSetting {
return toggleSetting{name: name}
}
func init() {
register([]backend.Command{
&ToggleSetting{},
&SetSetting{},
&ToggleSideBar{toggle("show_side_bar")},
&ToggleStatusBar{toggle("show_status_bar")},
&ToggleFullScreen{toggle("show_full_screen")},
&ToggleDistractionFree{toggle("show_distraction_free")},
&ToggleMinimap{toggle("show_minimap")},
&ToggleTabs{toggle("show_tabs")},
})
}