summaryrefslogtreecommitdiff
path: root/src/ui/sidebarwidget.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-13 07:42:26 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-13 07:42:26 +0300
commit61ce76bde91aaa6a5267e88577e10115f9627e5f (patch)
tree5b1c7c98580c7109d9a6862e72a671a43b4554d5 /src/ui/sidebarwidget.c
parentfab098d5701a55fd4a0b3fb74ae332797e3be377 (diff)
Toggle sidebar visibility; save width and mode
Diffstat (limited to 'src/ui/sidebarwidget.c')
-rw-r--r--src/ui/sidebarwidget.c115
1 files changed, 75 insertions, 40 deletions
diff --git a/src/ui/sidebarwidget.c b/src/ui/sidebarwidget.c
index 8ca2e448..a5e0e4ee 100644
--- a/src/ui/sidebarwidget.c
+++ b/src/ui/sidebarwidget.c
@@ -46,6 +46,7 @@ struct Impl_SidebarWidget {
46 enum iSidebarMode mode; 46 enum iSidebarMode mode;
47 iScrollWidget *scroll; 47 iScrollWidget *scroll;
48 int scrollY; 48 int scrollY;
49 int width;
49 iLabelWidget *modeButtons[max_SidebarMode]; 50 iLabelWidget *modeButtons[max_SidebarMode];
50 int itemHeight; 51 int itemHeight;
51 int maxButtonLabelWidth; 52 int maxButtonLabelWidth;
@@ -131,6 +132,14 @@ void setMode_SidebarWidget(iSidebarWidget *d, enum iSidebarMode mode) {
131 d->itemHeight = heights[mode] * lineHeight_Text(default_FontId); 132 d->itemHeight = heights[mode] * lineHeight_Text(default_FontId);
132} 133}
133 134
135enum iSidebarMode mode_SidebarWidget(const iSidebarWidget *d) {
136 return d->mode;
137}
138
139int width_SidebarWidget(const iSidebarWidget *d) {
140 return d->width;
141}
142
134static const char *normalModeLabels_[max_SidebarMode] = { 143static const char *normalModeLabels_[max_SidebarMode] = {
135 "\U0001f5b9 Outline", 144 "\U0001f5b9 Outline",
136 "\U0001f588 Bookmarks", 145 "\U0001f588 Bookmarks",
@@ -148,11 +157,15 @@ static const char *tightModeLabels_[max_SidebarMode] = {
148void init_SidebarWidget(iSidebarWidget *d) { 157void init_SidebarWidget(iSidebarWidget *d) {
149 iWidget *w = as_Widget(d); 158 iWidget *w = as_Widget(d);
150 init_Widget(w); 159 init_Widget(w);
160 setId_Widget(w, "sidebar");
151 setBackgroundColor_Widget(w, none_ColorId); 161 setBackgroundColor_Widget(w, none_ColorId);
152 setFlags_Widget(w, hover_WidgetFlag | arrangeHorizontal_WidgetFlag | resizeWidthOfChildren_WidgetFlag, iTrue); 162 setFlags_Widget(w,
163 hidden_WidgetFlag | hover_WidgetFlag | arrangeHorizontal_WidgetFlag |
164 resizeWidthOfChildren_WidgetFlag | collapse_WidgetFlag,
165 iTrue);
153 d->scrollY = 0; 166 d->scrollY = 0;
154 d->mode = -1; 167 d->mode = -1;
155 w->rect.size.x = 75 * gap_UI; 168 d->width = 75 * gap_UI;
156 init_Array(&d->items, sizeof(iSidebarItem)); 169 init_Array(&d->items, sizeof(iSidebarItem));
157 d->hoverItem = iInvalidPos; 170 d->hoverItem = iInvalidPos;
158 init_Click(&d->click, d, SDL_BUTTON_LEFT); 171 init_Click(&d->click, d, SDL_BUTTON_LEFT);
@@ -165,7 +178,8 @@ void init_SidebarWidget(iSidebarWidget *d) {
165 new_LabelWidget(normalModeLabels_[i], 0, 0, format_CStr("sidebar.mode arg:%d", i))), 178 new_LabelWidget(normalModeLabels_[i], 0, 0, format_CStr("sidebar.mode arg:%d", i))),
166 frameless_WidgetFlag | expand_WidgetFlag); 179 frameless_WidgetFlag | expand_WidgetFlag);
167 d->maxButtonLabelWidth = 180 d->maxButtonLabelWidth =
168 iMaxi(d->maxButtonLabelWidth, 3 * gap_UI + measure_Text(default_FontId, normalModeLabels_[i]).x); 181 iMaxi(d->maxButtonLabelWidth,
182 3 * gap_UI + measure_Text(default_FontId, normalModeLabels_[i]).x);
169 } 183 }
170 addChild_Widget(w, iClob(d->scroll = new_ScrollWidget())); 184 addChild_Widget(w, iClob(d->scroll = new_ScrollWidget()));
171 setThumb_ScrollWidget(d->scroll, 0, 0); 185 setThumb_ScrollWidget(d->scroll, 0, 0);
@@ -245,6 +259,21 @@ static void checkModeButtonLayout_SidebarWidget_(iSidebarWidget *d) {
245 } 259 }
246} 260}
247 261
262void setWidth_SidebarWidget(iSidebarWidget *d, int width) {
263 iWidget *w = as_Widget(d);
264 width = iMax(30 * gap_UI, width);
265 d->width = width;
266 if (isVisible_Widget(w)) {
267 w->rect.size.x = width;
268 }
269 arrange_Widget(findWidget_App("doctabs"));
270 checkModeButtonLayout_SidebarWidget_(d);
271 if (!isRefreshPending_App()) {
272 updateSize_DocumentWidget(document_App());
273 refresh_Widget(w);
274 }
275}
276
248static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) { 277static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) {
249 iWidget *w = as_Widget(d); 278 iWidget *w = as_Widget(d);
250 /* Handle commands. */ 279 /* Handle commands. */
@@ -252,51 +281,57 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev)
252 updateVisible_SidebarWidget_(d); 281 updateVisible_SidebarWidget_(d);
253 checkModeButtonLayout_SidebarWidget_(d); 282 checkModeButtonLayout_SidebarWidget_(d);
254 } 283 }
255 else if (isCommand_Widget(w, ev, "mouse.clicked")) { 284 else if (ev->type == SDL_USEREVENT && ev->user.code == command_UserEventCode) {
256 const char *cmd = command_UserEvent(ev); 285 const char *cmd = command_UserEvent(ev);
257 if (argLabel_Command(cmd, "button") == SDL_BUTTON_LEFT) { 286 if (isCommand_Widget(w, ev, "mouse.clicked")) {
258 if (arg_Command(cmd)) { 287 if (argLabel_Command(cmd, "button") == SDL_BUTTON_LEFT) {
259 setFlags_Widget(d->resizer, pressed_WidgetFlag, iTrue); 288 if (arg_Command(cmd)) {
260 setBackgroundColor_Widget(d->resizer, gray75_ColorId); 289 setFlags_Widget(d->resizer, pressed_WidgetFlag, iTrue);
261 setMouseGrab_Widget(d->resizer); 290 setBackgroundColor_Widget(d->resizer, gray75_ColorId);
262 refresh_Widget(d->resizer); 291 setMouseGrab_Widget(d->resizer);
263 } 292 refresh_Widget(d->resizer);
264 else { 293 }
265 setFlags_Widget(d->resizer, pressed_WidgetFlag, iFalse); 294 else {
266 setBackgroundColor_Widget(d->resizer, none_ColorId); 295 setFlags_Widget(d->resizer, pressed_WidgetFlag, iFalse);
267 setMouseGrab_Widget(NULL); 296 setBackgroundColor_Widget(d->resizer, none_ColorId);
268 refresh_Widget(d->resizer); 297 setMouseGrab_Widget(NULL);
298 refresh_Widget(d->resizer);
299 }
269 } 300 }
301 return iTrue;
270 } 302 }
271 return iTrue; 303 else if (isCommand_Widget(w, ev, "mouse.moved")) {
272 } 304 if (isResizing_SidebarWidget_(d)) {
273 else if (isCommand_Widget(w, ev, "mouse.moved")) { 305 const iInt2 local = localCoord_Widget(w, coord_Command(cmd));
274 const char *cmd = command_UserEvent(ev); 306 setWidth_SidebarWidget(d, local.x + d->resizer->rect.size.x / 2);
275 if (isResizing_SidebarWidget_(d)) {
276 const iInt2 local = localCoord_Widget(w, coord_Command(cmd));
277 w->rect.size.x = iMax(30 * gap_UI, local.x + d->resizer->rect.size.x / 2);
278 arrange_Widget(findWidget_App("doctabs"));
279 checkModeButtonLayout_SidebarWidget_(d);
280 if (!isRefreshPending_App()) {
281 updateSize_DocumentWidget(document_App());
282 refresh_Widget(w);
283 } 307 }
308 return iTrue;
284 } 309 }
285 return iTrue; 310 else if (equal_Command(cmd, "sidebar.width")) {
286 } 311 setWidth_SidebarWidget(d, arg_Command(cmd));
287 else if (isCommand_Widget(w, ev, "scroll.moved")) { 312 return iTrue;
288 d->scrollY = arg_Command(command_UserEvent(ev)); 313 }
289 d->hoverItem = iInvalidPos; 314 else if (equal_Command(cmd, "sidebar.mode")) {
290 refresh_Widget(w);
291 return iTrue;
292 }
293 else if (ev->type == SDL_USEREVENT && ev->user.code == command_UserEventCode) {
294 const char *cmd = command_UserEvent(ev);
295 if (isCommand_Widget(w, ev, "sidebar.mode")) {
296 setMode_SidebarWidget(d, arg_Command(cmd)); 315 setMode_SidebarWidget(d, arg_Command(cmd));
297 updateItems_SidebarWidget_(d); 316 updateItems_SidebarWidget_(d);
298 return iTrue; 317 return iTrue;
299 } 318 }
319 else if (equal_Command(cmd, "sidebar.toggle")) {
320 setFlags_Widget(w, hidden_WidgetFlag, isVisible_Widget(w));
321 if (isVisible_Widget(w)) {
322 w->rect.size.x = d->width;
323 }
324 arrange_Widget(w->parent);
325 updateSize_DocumentWidget(document_App());
326 refresh_Widget(w->parent);
327 return iTrue;
328 }
329 else if (equal_Command(cmd, "scroll.moved")) {
330 d->scrollY = arg_Command(command_UserEvent(ev));
331 d->hoverItem = iInvalidPos;
332 refresh_Widget(w);
333 return iTrue;
334 }
300 else if (equal_Command(cmd, "tabs.changed") || equal_Command(cmd, "document.changed")) { 335 else if (equal_Command(cmd, "tabs.changed") || equal_Command(cmd, "document.changed")) {
301 updateItems_SidebarWidget_(d); 336 updateItems_SidebarWidget_(d);
302 } 337 }