Skip to content

Commit 28edfe4

Browse files
committed
feat(cli, hotkey): change their respective defined processing methods into a unified calling method instead of scattered
1 parent 7112b46 commit 28edfe4

File tree

6 files changed

+66
-67
lines changed

6 files changed

+66
-67
lines changed

include/window_procedure/window_procedure.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ void CleanupBeforeTimerAction(void);
157157
*/
158158
BOOL StartCountdownWithTime(HWND hwnd, int seconds);
159159

160+
/**
161+
* @brief Toggle window visibility (show/hide)
162+
*/
163+
void ToggleWindowVisibility(HWND hwnd);
164+
165+
/**
166+
* @brief Toggle window topmost status
167+
*/
168+
void ToggleTopmost(HWND hwnd);
169+
170+
/**
171+
* @brief Toggle millisecond display
172+
*/
173+
void ToggleMilliseconds(HWND hwnd);
174+
160175
/* ============================================================================
161176
* Configuration Handlers
162177
* ============================================================================ */

src/cli.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,7 @@ static BOOL HandleQuickCountdown(HWND hwnd, const char* input) {
209209

210210
static BOOL HandleVisibility(HWND hwnd, const char* input) {
211211
(void)input;
212-
if (IsWindowVisible(hwnd)) {
213-
ShowWindow(hwnd, SW_HIDE);
214-
} else {
215-
ShowWindow(hwnd, SW_SHOW);
216-
SetForegroundWindow(hwnd);
217-
}
212+
ToggleWindowVisibility(hwnd);
218213
return TRUE;
219214
}
220215

@@ -234,7 +229,6 @@ static BOOL HandlePauseResume(HWND hwnd, const char* input) {
234229

235230
static BOOL HandleRestart(HWND hwnd, const char* input) {
236231
(void)input;
237-
CloseAllNotifications();
238232
RestartCurrentTimer(hwnd);
239233
return TRUE;
240234
}

src/window_procedure/window_commands.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static LRESULT CmdAbout(HWND hwnd, WPARAM wp, LPARAM lp) {
7171

7272
static LRESULT CmdToggleTopmost(HWND hwnd, WPARAM wp, LPARAM lp) {
7373
(void)wp; (void)lp; (void)hwnd;
74-
WriteConfigTopmost(!CLOCK_WINDOW_TOPMOST ? STR_TRUE : STR_FALSE);
74+
ToggleTopmost(hwnd);
7575
return 0;
7676
}
7777

@@ -185,7 +185,7 @@ static LRESULT CmdEditMode(HWND hwnd, WPARAM wp, LPARAM lp) {
185185

186186
static LRESULT CmdToggleVisibility(HWND hwnd, WPARAM wp, LPARAM lp) {
187187
(void)wp; (void)lp;
188-
PostMessage(hwnd, WM_HOTKEY, HOTKEY_ID_TOGGLE_VISIBILITY, 0);
188+
ToggleWindowVisibility(hwnd);
189189
return 0;
190190
}
191191

src/window_procedure/window_commands_timer.c

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,13 @@ LRESULT CmdCustomCountdown(HWND hwnd, WPARAM wp, LPARAM lp) {
5353

5454
LRESULT CmdShowCurrentTime(HWND hwnd, WPARAM wp, LPARAM lp) {
5555
(void)wp; (void)lp;
56-
CleanupBeforeTimerAction();
57-
58-
TimerMode mode = CLOCK_SHOW_CURRENT_TIME ? TIMER_MODE_COUNTDOWN : TIMER_MODE_SHOW_TIME;
59-
TimerModeParams params = {0, TRUE, FALSE, TRUE};
60-
SwitchTimerMode(hwnd, mode, &params);
61-
62-
KillTimer(hwnd, 1);
63-
ResetTimerWithInterval(hwnd);
56+
ToggleShowTimeMode(hwnd);
6457
return 0;
6558
}
6659

6760
LRESULT CmdCountUp(HWND hwnd, WPARAM wp, LPARAM lp) {
6861
(void)wp; (void)lp;
69-
CleanupBeforeTimerAction();
70-
71-
if (!CLOCK_COUNT_UP) {
72-
TimerModeParams params = {0, TRUE, FALSE, TRUE};
73-
SwitchTimerMode(hwnd, TIMER_MODE_COUNTUP, &params);
74-
} else {
75-
CLOCK_COUNT_UP = FALSE;
76-
}
77-
78-
KillTimer(hwnd, 1);
79-
ResetTimerWithInterval(hwnd);
80-
InvalidateRect(hwnd, NULL, TRUE);
62+
StartCountUp(hwnd);
8163
return 0;
8264
}
8365

@@ -125,8 +107,6 @@ LRESULT CmdPauseResume(HWND hwnd, WPARAM wp, LPARAM lp) {
125107

126108
LRESULT CmdRestartTimer(HWND hwnd, WPARAM wp, LPARAM lp) {
127109
(void)wp; (void)lp;
128-
CleanupBeforeTimerAction();
129-
CloseAllNotifications();
130110
RestartCurrentTimer(hwnd);
131111
return 0;
132112
}
@@ -143,13 +123,7 @@ LRESULT CmdTimeFormat(HWND hwnd, TimeFormatType format) {
143123

144124
LRESULT CmdToggleMilliseconds(HWND hwnd, WPARAM wp, LPARAM lp) {
145125
(void)wp; (void)lp;
146-
WriteConfigShowMilliseconds(!g_AppConfig.display.time_format.show_milliseconds);
147-
148-
/* Reset timer with new interval (10ms for milliseconds, 1000ms without) */
149-
extern void ResetTimerWithInterval(HWND hwnd);
150-
ResetTimerWithInterval(hwnd);
151-
152-
InvalidateRect(hwnd, NULL, TRUE);
126+
ToggleMilliseconds(hwnd);
153127
return 0;
154128
}
155129

@@ -222,20 +196,7 @@ LRESULT CmdSetCountdownTime(HWND hwnd, WPARAM wp, LPARAM lp) {
222196

223197
LRESULT CmdPomodoroStart(HWND hwnd, WPARAM wp, LPARAM lp) {
224198
(void)wp; (void)lp;
225-
CleanupBeforeTimerAction();
226-
227-
if (!IsWindowVisible(hwnd)) ShowWindow(hwnd, SW_SHOW);
228-
229-
extern void InitializePomodoro(void);
230-
InitializePomodoro();
231-
232-
CLOCK_SHOW_CURRENT_TIME = FALSE;
233-
CLOCK_COUNT_UP = FALSE;
234-
CLOCK_IS_PAUSED = FALSE;
235-
236-
KillTimer(hwnd, 1);
237-
ResetTimerWithInterval(hwnd);
238-
InvalidateRect(hwnd, NULL, TRUE);
199+
StartPomodoroTimer(hwnd);
239200
return 0;
240201
}
241202

src/window_procedure/window_hotkeys.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,15 @@ extern void WriteConfigShowMilliseconds(BOOL showMilliseconds);
3636
typedef void (*HotkeyAction)(HWND);
3737

3838
static void HotkeyToggleVisibility(HWND hwnd) {
39-
if (IsWindowVisible(hwnd)) {
40-
ShowWindow(hwnd, SW_HIDE);
41-
} else {
42-
ShowWindow(hwnd, SW_SHOW);
43-
SetForegroundWindow(hwnd);
44-
}
39+
ToggleWindowVisibility(hwnd);
4540
}
4641

4742
static void HotkeyRestartTimer(HWND hwnd) {
48-
CloseAllNotifications();
4943
RestartCurrentTimer(hwnd);
5044
}
5145

5246
static void HotkeyToggleMilliseconds(HWND hwnd) {
53-
WriteConfigShowMilliseconds(!g_AppConfig.display.time_format.show_milliseconds);
54-
55-
/* Reset timer with new interval (10ms for milliseconds, 1000ms without) */
56-
extern void ResetTimerWithInterval(HWND hwnd);
57-
ResetTimerWithInterval(hwnd);
58-
59-
InvalidateRect(hwnd, NULL, TRUE);
47+
ToggleMilliseconds(hwnd);
6048
}
6149

6250
static void HotkeyCustomCountdown(HWND hwnd) {

src/window_procedure/window_procedure.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,19 @@ void StartDefaultCountDown(HWND hwnd) {
291291

292292
void StartPomodoroTimer(HWND hwnd) {
293293
CleanupBeforeTimerAction();
294-
PostMessage(hwnd, WM_COMMAND, CLOCK_IDM_POMODORO_START, 0);
294+
295+
if (!IsWindowVisible(hwnd)) ShowWindow(hwnd, SW_SHOW);
296+
297+
extern void InitializePomodoro(void);
298+
InitializePomodoro();
299+
300+
CLOCK_SHOW_CURRENT_TIME = FALSE;
301+
CLOCK_COUNT_UP = FALSE;
302+
CLOCK_IS_PAUSED = FALSE;
303+
304+
KillTimer(hwnd, 1);
305+
ResetTimerWithInterval(hwnd);
306+
InvalidateRect(hwnd, NULL, TRUE);
295307
}
296308

297309
#define TIMER_ID_TRANSITION_END 100
@@ -316,6 +328,7 @@ void RestartCurrentTimer(HWND hwnd) {
316328
extern int countdown_elapsed_time, countup_elapsed_time;
317329
extern void ResetMillisecondAccumulator(void);
318330

331+
CloseAllNotifications(); // Centralized cleanup
319332
StopNotificationSound();
320333

321334
CleanupBeforeTimerAction();
@@ -400,3 +413,31 @@ BOOL StartCountdownWithTime(HWND hwnd, int seconds) {
400413
return result;
401414
}
402415

416+
void ToggleMilliseconds(HWND hwnd) {
417+
extern void WriteConfigShowMilliseconds(BOOL showMilliseconds);
418+
extern void ResetTimerWithInterval(HWND hwnd);
419+
420+
BOOL newState = !g_AppConfig.display.time_format.show_milliseconds;
421+
WriteConfigShowMilliseconds(newState);
422+
423+
/* Reset timer with new interval (10ms for milliseconds, 1000ms without) */
424+
ResetTimerWithInterval(hwnd);
425+
426+
InvalidateRect(hwnd, NULL, TRUE);
427+
}
428+
429+
void ToggleTopmost(HWND hwnd) {
430+
extern void WriteConfigTopmost(const char* value);
431+
// Use "true"/"false" literals to avoid dependency on specific header macros if not present
432+
WriteConfigTopmost(!CLOCK_WINDOW_TOPMOST ? "true" : "false");
433+
}
434+
435+
void ToggleWindowVisibility(HWND hwnd) {
436+
if (IsWindowVisible(hwnd)) {
437+
ShowWindow(hwnd, SW_HIDE);
438+
} else {
439+
ShowWindow(hwnd, SW_SHOW);
440+
SetForegroundWindow(hwnd);
441+
}
442+
}
443+

0 commit comments

Comments
 (0)