-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathTypes.hs
274 lines (241 loc) · 7.7 KB
/
Types.hs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
{-# LANGUAGE TemplateHaskell #-}
module Termonad.Types where
import Termonad.Prelude
import Control.Lens ((&), (.~), (^.), firstOf, makeLensesFor)
import Data.Unique (Unique, hashUnique, newUnique)
import GI.Gtk
( Application
, ApplicationWindow
, Label
, Notebook
, ScrolledWindow
)
import GI.Pango (FontDescription)
import GI.Vte (Terminal)
import Text.Pretty.Simple (pPrint)
import Text.Show (Show(showsPrec), ShowS, showParen, showString)
import Termonad.Config (TMConfig)
import Termonad.FocusList (FocusList, emptyFL, focusItemGetter, singletonFL)
data TMTerm = TMTerm
{ term :: !Terminal
, pid :: !Int
, unique :: !Unique
}
instance Show TMTerm where
showsPrec :: Int -> TMTerm -> ShowS
showsPrec d TMTerm{..} =
showParen (d > 10) $
showString "TMTerm {" .
showString "term = " .
showString "(GI.GTK.Terminal)" .
showString ", " .
showString "pid = " .
showsPrec (d + 1) pid .
showString ", " .
showString "unique = " .
showsPrec (d + 1) (hashUnique unique) .
showString "}"
$(makeLensesFor
[ ("term", "lensTerm")
, ("pid", "lensPid")
, ("unique", "lensUnique")
]
''TMTerm
)
data TMNotebookTab = TMNotebookTab
{ tmNotebookTabTermContainer :: !ScrolledWindow
, tmNotebookTabTerm :: !TMTerm
, tmNotebookTabLabel :: !Label
}
instance Show TMNotebookTab where
showsPrec :: Int -> TMNotebookTab -> ShowS
showsPrec d TMNotebookTab{..} =
showParen (d > 10) $
showString "TMNotebookTab {" .
showString "tmNotebookTabTermContainer = " .
showString "(GI.GTK.ScrolledWindow)" .
showString ", " .
showString "tmNotebookTabTerm = " .
showsPrec (d + 1) tmNotebookTabTerm .
showString ", " .
showString "tmNotebookTabLabel = " .
showString "(GI.GTK.Label)" .
showString "}"
$(makeLensesFor
[ ("tmNotebookTabTermContainer", "lensTMNotebookTabTermContainer")
, ("tmNotebookTabTerm", "lensTMNotebookTabTerm")
, ("tmNotebookTabLabel", "lensTMNotebookTabLabel")
]
''TMNotebookTab
)
data TMNotebook = TMNotebook
{ tmNotebook :: !Notebook
, tmNotebookTabs :: !(FocusList TMNotebookTab)
}
instance Show TMNotebook where
showsPrec :: Int -> TMNotebook -> ShowS
showsPrec d TMNotebook{..} =
showParen (d > 10) $
showString "TMNotebook {" .
showString "tmNotebook = " .
showString "(GI.GTK.Notebook)" .
showString ", " .
showString "tmNotebookTabs = " .
showsPrec (d + 1) tmNotebookTabs .
showString "}"
$(makeLensesFor
[ ("tmNotebook", "lensTMNotebook")
, ("tmNotebookTabs", "lensTMNotebookTabs")
]
''TMNotebook
)
data UserRequestedExit = UserRequestedExit | UserDidNotRequestExit deriving (Eq, Show)
data TMState' = TMState
{ tmStateApp :: !Application
, tmStateAppWin :: !ApplicationWindow
, tmStateNotebook :: !TMNotebook
, tmStateFontDesc :: !FontDescription
, tmStateConfig :: !TMConfig
, tmStateUserReqExit :: !UserRequestedExit
-- ^ This signifies whether or not the user has requested that Termonad
-- exit by either closing all terminals or clicking the exit button. If so,
-- 'tmStateUserReqExit' should have a value of 'UserRequestedExit'. However,
-- if the window manager requested Termonad to exit (probably through the user
-- trying to close Termonad through their window manager), then this will be
-- set to 'UserDidNotRequestExit'.
}
instance Show TMState' where
showsPrec :: Int -> TMState' -> ShowS
showsPrec d TMState{..} =
showParen (d > 10) $
showString "TMState {" .
showString "tmStateApp = " .
showString "(GI.GTK.Application)" .
showString ", " .
showString "tmStateAppWin = " .
showString "(GI.GTK.ApplicationWindow)" .
showString ", " .
showString "tmStateNotebook = " .
showsPrec (d + 1) tmStateNotebook .
showString ", " .
showString "tmStateFontDesc = " .
showString "(GI.Pango.FontDescription)" .
showString ", " .
showString "tmStateConfig = " .
showsPrec (d + 1) tmStateConfig .
showString ", " .
showString "tmStateUserReqExit = " .
showsPrec (d + 1) tmStateUserReqExit .
showString "}"
$(makeLensesFor
[ ("tmStateApp", "lensTMStateApp")
, ("tmStateAppWin", "lensTMStateAppWin")
, ("tmStateNotebook", "lensTMStateNotebook")
, ("tmStateFontDesc", "lensTMStateFontDesc")
, ("tmStateConfig", "lensTMStateConfig")
, ("tmStateUserReqExit", "lensTMStateUserReqExit")
]
''TMState'
)
type TMState = MVar TMState'
instance Eq TMTerm where
(==) :: TMTerm -> TMTerm -> Bool
(==) = (==) `on` (unique :: TMTerm -> Unique)
instance Eq TMNotebookTab where
(==) :: TMNotebookTab -> TMNotebookTab -> Bool
(==) = (==) `on` tmNotebookTabTerm
createTMTerm :: Terminal -> Int -> Unique -> TMTerm
createTMTerm trm pd unq =
TMTerm
{ term = trm
, pid = pd
, unique = unq
}
newTMTerm :: Terminal -> Int -> IO TMTerm
newTMTerm trm pd = do
unq <- newUnique
pure $ createTMTerm trm pd unq
createTMNotebookTab :: Label -> ScrolledWindow -> TMTerm -> TMNotebookTab
createTMNotebookTab tabLabel scrollWin trm =
TMNotebookTab
{ tmNotebookTabTermContainer = scrollWin
, tmNotebookTabTerm = trm
, tmNotebookTabLabel = tabLabel
}
createTMNotebook :: Notebook -> FocusList TMNotebookTab -> TMNotebook
createTMNotebook note tabs =
TMNotebook
{ tmNotebook = note
, tmNotebookTabs = tabs
}
createEmptyTMNotebook :: Notebook -> TMNotebook
createEmptyTMNotebook notebook = createTMNotebook notebook emptyFL
newTMState :: TMConfig -> Application -> ApplicationWindow -> TMNotebook -> FontDescription -> IO TMState
newTMState tmConfig app appWin note fontDesc =
newMVar $
TMState
{ tmStateApp = app
, tmStateAppWin = appWin
, tmStateNotebook = note
, tmStateFontDesc = fontDesc
, tmStateConfig = tmConfig
, tmStateUserReqExit = UserDidNotRequestExit
}
newEmptyTMState :: TMConfig -> Application -> ApplicationWindow -> Notebook -> FontDescription -> IO TMState
newEmptyTMState tmConfig app appWin note fontDesc =
newMVar $
TMState
{ tmStateApp = app
, tmStateAppWin = appWin
, tmStateNotebook = createEmptyTMNotebook note
, tmStateFontDesc = fontDesc
, tmStateConfig = tmConfig
, tmStateUserReqExit = UserDidNotRequestExit
}
newTMStateSingleTerm ::
TMConfig
-> Application
-> ApplicationWindow
-> Notebook
-> Label
-> ScrolledWindow
-> Terminal
-> Int
-> FontDescription
-> IO TMState
newTMStateSingleTerm tmConfig app appWin note label scrollWin trm pd fontDesc = do
tmTerm <- newTMTerm trm pd
let tmNoteTab = createTMNotebookTab label scrollWin tmTerm
tabs = singletonFL tmNoteTab
tmNote = createTMNotebook note tabs
newTMState tmConfig app appWin tmNote fontDesc
traceShowMTMState :: TMState -> IO ()
traceShowMTMState mvarTMState = do
tmState <- readMVar mvarTMState
print tmState
pTraceShowMTMState :: TMState -> IO ()
pTraceShowMTMState mvarTMState = do
tmState <- readMVar mvarTMState
pPrint tmState
getFocusedTermFromState :: TMState -> IO (Maybe Terminal)
getFocusedTermFromState mvarTMState = do
withMVar
mvarTMState
( pure .
firstOf
( lensTMStateNotebook .
lensTMNotebookTabs .
focusItemGetter .
traverse .
lensTMNotebookTabTerm .
lensTerm
)
)
setUserRequestedExit :: TMState -> IO ()
setUserRequestedExit mvarTMState = do
modifyMVar_ mvarTMState $ \tmState -> do
pure $ tmState & lensTMStateUserReqExit .~ UserRequestedExit
getUserRequestedExit :: TMState -> IO UserRequestedExit
getUserRequestedExit mvarTMState = do
tmState <- readMVar mvarTMState
pure $ tmState ^. lensTMStateUserReqExit