suwako / UnixCalc アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

ボタン押すフィードバックの修正

諏訪子 <suwako@076.moe>、2026-01-19 22:23
コミット 70facb3bc92339c477c1be8997cc6aa9f4d51d07 ・親 0abfbe1 ・ファイルを見る

4個のファイル、+64 -55

変更 control.c
@@ -53,6 +53,8 @@
5353
5454 char curinput[64] = {0};
5555 int input_pos = 0;
56+int initialized = 0;
57+SuwaButton buttons[NUM_ROWS][NUM_COLS];
5658
5759 static const char *btn_labels[][10] = {
5860 { NULL }, // 数字表示
@@ -102,7 +104,8 @@
102104 found.width = btn_w;
103105 found.height = btn_h;
104106 found.text = btn_labels[row][col];
105- found.pressed = 1;
107+ found.row = row;
108+ found.col = col;
106109
107110 return &found;
108111 }
@@ -169,7 +172,35 @@
169172 return res;
170173 }
171174
172-void control_expose(SuwaWindow *window, CtrlLabels *labels) {
175+void initialize_basic_buttons(SuwaWindow *window, XftDraw *backdraw, float scale,
176+ SuwaButton *but) {
177+ int bw = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
178+ int bh = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
179+ int bp = (int)(DESIGN_PADDING * scale + .5f);
180+ int by = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
181+
182+ XftColor buttonColor;
183+ buttonColor = suwaui_set_button_fgcolor(window, COLOR_BS);
184+
185+ for (int row = 1; row < NUM_ROWS; ++row) {
186+ for (int col = 0; col < NUM_COLS; ++col) {
187+ SuwaButton btn = suwaui_add_button(bp + col * (bw + bp),
188+ by + (row - 1) * (bh + bp),
189+ bw, bh, btn_labels[row][col], window->font, COLOR_ND, buttonColor);
190+
191+ if (but != NULL && but->text != NULL) {
192+ if (strcmp(but->text, btn.text) == 0) {
193+ if (but->pressed == 1) btn.pressed = 1;
194+ else if (but->hovered == 1) btn.hovered = 1;
195+ }
196+ }
197+ suwaui_draw_button(window, &btn, backdraw);
198+ buttons[row][col] = btn;
199+ }
200+ }
201+}
202+
203+void control_expose(SuwaWindow *window, CtrlLabels *labels, SuwaButton *button) {
173204 if (window->event.type != Expose && window->event.type != ConfigureNotify) return;
174205
175206 XWindowAttributes attr;
@@ -194,7 +225,7 @@
194225 return;
195226 }
196227
197- XSetForeground(window->display, window->gc, BGCOL);
228+ XSetForeground(window->display, window->gc, COLOR_BD);
198229 XFillRectangle(window->display, window->backbuf, window->gc, 0, 0, w, h);
199230
200231 float scale = get_scale(window);
@@ -231,35 +262,15 @@
231262 tx, ty, (const FcChar8 *)labels->problem->text, 64);
232263 }
233264
234- int bw = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
235- int bh = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
236- int bp = (int)(DESIGN_PADDING * scale + .5f);
237- int by = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
265+ initialize_basic_buttons(window, backdraw, scale, button);
238266
239- XftColor buttonColor;
240-#if defined(__OpenBSD__)
241- buttonColor = suwaui_set_button_fgcolor(window, "#232320");
242-#elif defined(__FreeBSD__)
243- buttonColor = suwaui_set_button_fgcolor(window, "#232020");
244-#endif
245-
246- for (int row = 1; row < NUM_ROWS; ++row) {
247- for (int col = 0; col < NUM_COLS; ++col) {
248- SuwaButton btn = suwaui_add_button(bp + col * (bw + bp),
249- by + (row - 1) * (bh + bp),
250- bw, bh, btn_labels[row][col], window->font, BTCOL, buttonColor);
251-
252- suwaui_draw_button(window, &btn, backdraw);
253- }
254- }
255-
256267 XCopyArea(window->display, window->backbuf, window->xwindow, window->gc,
257268 0, 0, w, h, 0, 0);
258269 XftDrawDestroy(backdraw);
259270 XFlush(window->display);
260271 }
261272
262-void handle_button_press(SuwaWindow *window, int mx, int my) {
273+void handle_button_press(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
263274 SuwaButton *btn = find_button_at(window, mx, my);
264275 if (!btn) return;
265276
@@ -274,8 +285,8 @@
274285 return;
275286 }
276287
277- suwaui_draw_button(window, btn, backdraw);
278- XFlush(window->display);
288+ window->event = (XEvent){.type = Expose};
289+ control_expose(window, labels, btn);
279290 }
280291
281292 void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
@@ -293,11 +304,6 @@
293304 return;
294305 }
295306
296- window->target = window->backbuf;
297- suwaui_draw_button(window, btn, backdraw);
298- XftDrawDestroy(backdraw);
299- XFlush(window->display);
300-
301307 const char *label = btn->text;
302308 if (strcmp(label, "C") == 0) {
303309 clear_calculator(labels);
@@ -317,7 +323,7 @@
317323 }
318324
319325 window->event = (XEvent){.type = Expose};
320- control_expose(window, labels);
326+ control_expose(window, labels, btn);
321327 }
322328
323329 void handle_key_press(SuwaWindow *window, CtrlLabels *labels) {
@@ -396,23 +402,26 @@
396402
397403 redraw:
398404 window->event = (XEvent){.type = Expose};
399- control_expose(window, labels);
405+ control_expose(window, labels, &(SuwaButton){0});
400406 }
401407
402-// void handle_mouse_hover(SuwaWindow *window) {
403-// int x = window->event,xmotion.x;
404-// int y = window->event,xmotion.y;
405-// SuwaButton *hover = find_button_at(x, y);
408+void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels) {
409+ int x = window->event.xmotion.x;
410+ int y = window->event.xmotion.y;
411+ SuwaButton *btn = find_button_at(window, x, y);
412+ if (!btn) return;
406413
407-// if (hover != hovered_btn) {
408-// if (hovered_btn) {
409-// hovered_btn->pressed = 0;
410-// redraw_single_button(&window, hovered_btn);
411-// }
412-// hovered_btn = hover;
414+ btn->hovered = 1;
415+ XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf,
416+ DefaultVisual(window->display, DefaultScreen(window->display)),
417+ DefaultColormap(window->display, DefaultScreen(window->display)));
418+ if (!backdraw) {
419+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
420+ XFreePixmap(window->display, window->backbuf);
421+ window->backbuf = None;
422+ return;
423+ }
413424
414-// if (hovered_btn) {
415-// redraw_single_button(&window, hovered_btn);
416-// }
417-// }
418-// }
425+ window->event = (XEvent){.type = Expose};
426+ control_expose(window, labels, btn);
427+}
変更 control.h
@@ -47,4 +47,4 @@
4747 void handle_button_press(SuwaWindow *window, CtrlLabels *labels, int mx, int my);
4848 void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my);
4949 void handle_key_press(SuwaWindow *window, CtrlLabels *labels);
50-// void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels);
50+void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels);
@@ -332,8 +332,8 @@
332332 | ButtonPressMask
333333 | ButtonReleaseMask
334334 | KeyPressMask
335- | PointerMotionMask
336- | ButtonMotionMask
335+ // | PointerMotionMask
336+ // | ButtonMotionMask
337337 // | StructureNotifyMask
338338 );
339339
変更 main.c
@@ -108,9 +108,9 @@
108108 case KeyPress:
109109 handle_key_press(&window, &lbl);
110110 break;
111- // case MotionNotify:
112- // handle_mouse_hover(&window, &lbl);
113- // break;
111+ case MotionNotify:
112+ handle_mouse_hover(&window, &lbl);
113+ break;
114114 case ClientMessage:
115115 // WM_DELETE_WINDOW
116116 break;