Debian Codegraph @

ui/ui.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package ui
18
19 import (
20 "os"
21 "os/exec"
22
23 "github.com/zyedidia/clipboard"
24
25 "go.mau.fi/mauview"
•26 "github.com/gdamore/tcell/v2"
27
28 ifc "maunium.net/go/gomuks/interface"
29 )
30
31 type View string
32
33 // Allowed views in GomuksUI
34 const (
35 ViewLogin View = "login"
36 ViewMain View = "main"
37 )
38
39 type GomuksUI struct {
40 gmx ifc.Gomuks
41 app *mauview.Application
42
43 mainView *MainView
44 loginView *LoginView
45
46 views map[View]mauview.Component
47 }
48
49 func init() {
50 mauview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
51 mauview.Styles.PrimaryTextColor = tcell.ColorDefault
52 mauview.Styles.BorderColor = tcell.ColorDefault
53 mauview.Styles.ContrastBackgroundColor = tcell.ColorDarkGreen
54 if tcellDB := os.Getenv("TCELLDB"); len(tcellDB) == 0 {
55 if info, err := os.Stat("/usr/share/tcell/database"); err == nil && info.IsDir() {
56 os.Setenv("TCELLDB", "/usr/share/tcell/database")
57 }
58 }
59 }
60
61 func NewGomuksUI(gmx ifc.Gomuks) ifc.GomuksUI {
62 ui := &GomuksUI{
63 gmx: gmx,
64 app: mauview.NewApplication(),
65 }
66 return ui
67 }
68
69 func (ui *GomuksUI) Init() {
70 mauview.Backspace2RemovesWord = ui.gmx.Config().Backspace2RemovesWord
71 mauview.Backspace1RemovesWord = ui.gmx.Config().Backspace1RemovesWord
72 ui.app.SetAlwaysClear(ui.gmx.Config().AlwaysClearScreen)
73 clipboard.Initialize()
74 ui.views = map[View]mauview.Component{
75 ViewLogin: ui.NewLoginView(),
76 ViewMain: ui.NewMainView(),
77 }
78 ui.SetView(ViewLogin)
79 }
80
81 func (ui *GomuksUI) Start() error {
82 return ui.app.Start()
83 }
84
85 func (ui *GomuksUI) Stop() {
86 ui.app.Stop()
87 }
88
89 func (ui *GomuksUI) Finish() {
90 ui.app.ForceStop()
91 }
92
93 func (ui *GomuksUI) Render() {
94 ui.app.Redraw()
95 }
96
97 func (ui *GomuksUI) OnLogin() {
98 ui.SetView(ViewMain)
99 }
100
101 func (ui *GomuksUI) OnLogout() {
102 ui.SetView(ViewLogin)
103 }
104
105 func (ui *GomuksUI) HandleNewPreferences() {
106 ui.Render()
107 }
108
109 func (ui *GomuksUI) SetView(name View) {
110 ui.app.SetRoot(ui.views[name])
111 }
112
113 func (ui *GomuksUI) MainView() ifc.MainView {
114 return ui.mainView
115 }
116
117 func (ui *GomuksUI) RunExternal(executablePath string, args ...string) error {
118 callback := make(chan error)
119 ui.app.Suspend(func() {
120 cmd := exec.Command(executablePath, args...)
121 cmd.Stdout = os.Stdout
122 cmd.Stderr = os.Stderr
123 cmd.Stdin = os.Stdin
124 cmd.Env = os.Environ()
125 callback <- cmd.Run()
126 })
127 return <-callback
128 }