summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-12 16:28:53 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-12 16:28:53 +0300
commitbcd864988cefc34ea1bcb963e8f37f7bd3717481 (patch)
tree84d00d78f7bed3256f58e0e3a779607b57cee47c /src
parent7eea6f318d2252ccd92f6ed50e0320c143c40951 (diff)
SidebarWidget: Tight layout mode on resize; wheel scrolling
Diffstat (limited to 'src')
-rw-r--r--src/ui/sidebarwidget.c54
1 files changed, 43 insertions, 11 deletions
diff --git a/src/ui/sidebarwidget.c b/src/ui/sidebarwidget.c
index c5e7d05f..9f733426 100644
--- a/src/ui/sidebarwidget.c
+++ b/src/ui/sidebarwidget.c
@@ -48,6 +48,7 @@ struct Impl_SidebarWidget {
48 int scrollY; 48 int scrollY;
49 iLabelWidget *modeButtons[max_SidebarMode]; 49 iLabelWidget *modeButtons[max_SidebarMode];
50 int itemHeight; 50 int itemHeight;
51 int maxButtonLabelWidth;
51 iArray items; 52 iArray items;
52 size_t hoverItem; 53 size_t hoverItem;
53 iClick click; 54 iClick click;
@@ -130,29 +131,41 @@ void setMode_SidebarWidget(iSidebarWidget *d, enum iSidebarMode mode) {
130 d->itemHeight = heights[mode] * lineHeight_Text(default_FontId); 131 d->itemHeight = heights[mode] * lineHeight_Text(default_FontId);
131} 132}
132 133
134static const char *normalModeLabels_[max_SidebarMode] = {
135 "\U0001f5b9 Outline",
136 "\U0001f588 Bookmarks",
137 "\U0001f553 History",
138 "\U0001f464 Identities",
139};
140
141static const char *tightModeLabels_[max_SidebarMode] = {
142 "\U0001f5b9",
143 "\U0001f588",
144 "\U0001f553",
145 "\U0001f464",
146};
147
133void init_SidebarWidget(iSidebarWidget *d) { 148void init_SidebarWidget(iSidebarWidget *d) {
134 iWidget *w = as_Widget(d); 149 iWidget *w = as_Widget(d);
135 init_Widget(w); 150 init_Widget(w);
136 setBackgroundColor_Widget(w, none_ColorId); 151 setBackgroundColor_Widget(w, none_ColorId);
137 setFlags_Widget(w, hover_WidgetFlag | arrangeHorizontal_WidgetFlag, iTrue); 152 setFlags_Widget(w, hover_WidgetFlag | arrangeHorizontal_WidgetFlag | resizeWidthOfChildren_WidgetFlag, iTrue);
138 d->scrollY = 0; 153 d->scrollY = 0;
139 d->mode = -1; 154 d->mode = -1;
140 w->rect.size.x = 100 * gap_UI; 155 w->rect.size.x = 75 * gap_UI;
141 init_Array(&d->items, sizeof(iSidebarItem)); 156 init_Array(&d->items, sizeof(iSidebarItem));
142 d->hoverItem = iInvalidPos; 157 d->hoverItem = iInvalidPos;
143 init_Click(&d->click, d, SDL_BUTTON_LEFT); 158 init_Click(&d->click, d, SDL_BUTTON_LEFT);
144 setFlags_Widget(w, fixedWidth_WidgetFlag, iTrue); 159 setFlags_Widget(w, fixedWidth_WidgetFlag, iTrue);
145 const char *buttonLabels[max_SidebarMode] = { 160 d->maxButtonLabelWidth = 0;
146 "\U0001f5b9 Outline",
147 "\U0001f588 Bookmarks",
148 "\U0001f553 History",
149 "\U0001f464 Identities",
150 };
151 for (int i = 0; i < max_SidebarMode; i++) { 161 for (int i = 0; i < max_SidebarMode; i++) {
152 d->modeButtons[i] = addChildFlags_Widget( 162 d->modeButtons[i] = addChildFlags_Widget(
153 w, 163 w,
154 iClob(new_LabelWidget(buttonLabels[i], 0, 0, format_CStr("sidebar.mode arg:%d", i))), 164 iClob(
155 frameless_WidgetFlag); 165 new_LabelWidget(normalModeLabels_[i], 0, 0, format_CStr("sidebar.mode arg:%d", i))),
166 frameless_WidgetFlag | expand_WidgetFlag);
167 d->maxButtonLabelWidth =
168 iMaxi(d->maxButtonLabelWidth, 3 * gap_UI + measure_Text(default_FontId, normalModeLabels_[i]).x);
156 } 169 }
157 addChild_Widget(w, iClob(d->scroll = new_ScrollWidget())); 170 addChild_Widget(w, iClob(d->scroll = new_ScrollWidget()));
158 setThumb_ScrollWidget(d->scroll, 0, 0); 171 setThumb_ScrollWidget(d->scroll, 0, 0);
@@ -217,11 +230,27 @@ static void scroll_SidebarWidget_(iSidebarWidget *d, int offset) {
217 refresh_Widget(as_Widget(d)); 230 refresh_Widget(as_Widget(d));
218} 231}
219 232
233static void checkModeButtonLayout_SidebarWidget_(iSidebarWidget *d) {
234 const iBool isTight =
235 (width_Rect(bounds_Widget(as_Widget(d->modeButtons[0]))) < d->maxButtonLabelWidth);
236 for (int i = 0; i < max_SidebarMode; i++) {
237 if (isTight && ~flags_Widget(as_Widget(d->modeButtons[i])) & tight_WidgetFlag) {
238 setFlags_Widget(as_Widget(d->modeButtons[i]), tight_WidgetFlag, iTrue);
239 updateTextCStr_LabelWidget(d->modeButtons[i], tightModeLabels_[i]);
240 }
241 else if (!isTight && flags_Widget(as_Widget(d->modeButtons[i])) & tight_WidgetFlag) {
242 setFlags_Widget(as_Widget(d->modeButtons[i]), tight_WidgetFlag, iFalse);
243 updateTextCStr_LabelWidget(d->modeButtons[i], normalModeLabels_[i]);
244 }
245 }
246}
247
220static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) { 248static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev) {
221 iWidget *w = as_Widget(d); 249 iWidget *w = as_Widget(d);
222 /* Handle commands. */ 250 /* Handle commands. */
223 if (isResize_UserEvent(ev)) { 251 if (isResize_UserEvent(ev)) {
224 updateVisible_SidebarWidget_(d); 252 updateVisible_SidebarWidget_(d);
253 checkModeButtonLayout_SidebarWidget_(d);
225 } 254 }
226 else if (isCommand_Widget(w, ev, "mouse.clicked")) { 255 else if (isCommand_Widget(w, ev, "mouse.clicked")) {
227 const char *cmd = command_UserEvent(ev); 256 const char *cmd = command_UserEvent(ev);
@@ -247,6 +276,7 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev)
247 const iInt2 local = localCoord_Widget(w, coord_Command(cmd)); 276 const iInt2 local = localCoord_Widget(w, coord_Command(cmd));
248 w->rect.size.x = local.x + d->resizer->rect.size.x / 2; 277 w->rect.size.x = local.x + d->resizer->rect.size.x / 2;
249 arrange_Widget(findWidget_App("doctabs")); 278 arrange_Widget(findWidget_App("doctabs"));
279 checkModeButtonLayout_SidebarWidget_(d);
250 if (!isRefreshPending_App()) { 280 if (!isRefreshPending_App()) {
251 updateSize_DocumentWidget(document_App()); 281 updateSize_DocumentWidget(document_App());
252 refresh_Widget(w); 282 refresh_Widget(w);
@@ -292,6 +322,8 @@ static iBool processEvent_SidebarWidget_(iSidebarWidget *d, const SDL_Event *ev)
292#if defined (iPlatformApple) 322#if defined (iPlatformApple)
293 /* Momentum scrolling. */ 323 /* Momentum scrolling. */
294 scroll_SidebarWidget_(d, -ev->wheel.y * get_Window()->pixelRatio); 324 scroll_SidebarWidget_(d, -ev->wheel.y * get_Window()->pixelRatio);
325#else
326 scroll_SidebarWidget_(d, -ev->wheel.y * 3 * d->itemHeight);
295#endif 327#endif
296 d->hoverItem = iInvalidPos; 328 d->hoverItem = iInvalidPos;
297 refresh_Widget(w); 329 refresh_Widget(w);
@@ -328,10 +360,10 @@ static void draw_SidebarWidget_(const iSidebarWidget *d) {
328 const iSidebarItem *item = constAt_Array(&d->items, i); 360 const iSidebarItem *item = constAt_Array(&d->items, i);
329 const iRect itemRect = { pos, init_I2(width_Rect(bounds), d->itemHeight) }; 361 const iRect itemRect = { pos, init_I2(width_Rect(bounds), d->itemHeight) };
330 const iBool isHover = (d->hoverItem == i); 362 const iBool isHover = (d->hoverItem == i);
363 setClip_Paint(&p, intersect_Rect(itemRect, bounds));
331 if (isHover) { 364 if (isHover) {
332 fillRect_Paint(&p, itemRect, isPressing ? orange_ColorId : teal_ColorId); 365 fillRect_Paint(&p, itemRect, isPressing ? orange_ColorId : teal_ColorId);
333 } 366 }
334 setClip_Paint(&p, intersect_Rect(itemRect, bounds));
335 if (d->mode == documentOutline_SidebarMode) { 367 if (d->mode == documentOutline_SidebarMode) {
336 const int fg = isHover ? (isPressing ? black_ColorId : white_ColorId) : 368 const int fg = isHover ? (isPressing ? black_ColorId : white_ColorId) :
337 (item->indent == 0 ? white_ColorId : gray75_ColorId); 369 (item->indent == 0 ? white_ColorId : gray75_ColorId);