AmendHub

jcs

/

amend

/

amendments

/

83

*: Use new focusable API


jcs made amendment 83 about 1 month ago
--- amend.h Mon Jan 10 14:12:36 2022 +++ amend.h Tue Aug 16 10:54:26 2022 @@ -35,11 +35,12 @@ #define REPO_MENU_ID 131 #define REPO_MENU_ADD_FILE_ID 1 -#define REPO_MENU_REVERT_FILE_ID 2 +#define REPO_MENU_DISCARD_CHANGES_ID 2 #define REPO_MENU_APPLY_PATCH_ID 3 #define COMMIT_MENU_ID 132 -#define COMMIT_MENU_EXPORT_ID 1 +#define COMMIT_MENU_EDIT_ID 1 +#define COMMIT_MENU_EXPORT_ID 2 #define COMMIT_LDEF_ID 128 @@ -52,4 +53,6 @@ #define STR_AUTHOR_ID 128 #define STR_TABWIDTH_ID 129 -extern MenuHandle file_menu, edit_menu, repo_menu, commit_menu; +extern MenuHandle file_menu, edit_menu, repo_menu, commit_menu; + +void menu_defaults(void); --- browser.c Wed Jun 15 22:39:15 2022 +++ browser.c Tue Aug 16 14:34:48 2022 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 joshua stein <jcs@jcs.org> + * Copyright (c) 2021-2022 joshua stein <jcs@jcs.org> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,21 +22,35 @@ #include "browser.h" #include "committer.h" #include "diff.h" +#include "focusable.h" #include "patch.h" #include "repo.h" #include "settings.h" #include "tetab.h" #include "util.h" +bool browser_close(struct focusable *focusable); +void browser_idle(struct focusable *focusable, EventRecord *event); +void browser_update_menu(struct browser *browser); +void browser_update(struct focusable *focusable, EventRecord *event); +void browser_show_commit(struct browser *browser, + struct repo_commit *commit); +void browser_key_down(struct focusable *focusable, EventRecord *event); +void browser_mouse_down(struct focusable *focusable, EventRecord *event); +bool browser_handle_menu(struct focusable *focusable, short menu, + short item); + void browser_add_files(struct browser *browser); void browser_filter_commits(struct browser *browser); -void browser_revert_file(struct browser *browser); +void browser_discard_changes(struct browser *browser); Pattern fill_pattern; void -browser_idle(struct browser *browser) +browser_idle(struct focusable *focusable, EventRecord *event) { + struct browser *browser = (struct browser *)focusable->cookie; + switch (browser->state) { case BROWSER_STATE_IDLE: break; @@ -56,18 +70,12 @@ browser_idle(struct browser *browser) break; case BROWSER_STATE_OPEN_COMMITTER: committer_init(browser); - browser->state = BROWSER_STATE_COMMITTER_DO_DIFF; - break; - case BROWSER_STATE_COMMITTER_DO_DIFF: - committer_generate_diff(browser->committer); browser->state = BROWSER_STATE_WAITING_FOR_COMMITTER; break; case BROWSER_STATE_WAITING_FOR_COMMITTER: - if (browser->committer) - committer_idle(browser->committer); break; - case BROWSER_STATE_REVERT_FILE: - browser_revert_file(browser); + case BROWSER_STATE_DISCARD_CHANGES: + browser_discard_changes(browser); browser->state = BROWSER_STATE_IDLE; break; case BROWSER_STATE_EXPORT_PATCH: @@ -77,6 +85,7 @@ browser_idle(struct browser *browser) case BROWSER_STATE_APPLY_PATCH: browser_apply_patch(browser); browser->state = BROWSER_STATE_IDLE; + break; } } @@ -85,6 +94,7 @@ browser_init(struct repo *repo) { char title[256], filename[256], *justfilename; struct browser *browser; + struct focusable *focusable; Rect bounds = { 0 }, te_bounds = { 0 }; Rect data_bounds = { 0, 0, 0, 1 }; /* tlbr */ Point cell_size = { 0, 0 }; @@ -180,15 +190,27 @@ browser_init(struct repo *repo) browser_update_menu(browser); browser_add_files(browser); UpdateScrollbarForTE(browser->diff_scroller, browser->diff_te, true); + + focusable = xmalloczero(sizeof(struct focusable)); + focusable->cookie = browser; + focusable->win = browser->win; + focusable->idle = browser_idle; + focusable->update = browser_update; + focusable->mouse_down = browser_mouse_down; + focusable->menu = browser_handle_menu; + focusable->close = browser_close; + focusable_add(focusable); + progress(NULL); - ShowWindow(browser->win); return browser; } -void -browser_close(struct browser *browser) +bool +browser_close(struct focusable *focusable) { + struct browser *browser = (struct browser *)focusable->cookie; + if (browser->committer) browser_close_committer(browser); @@ -199,14 +221,22 @@ browser_close(struct browser *browser) DisposeWindow(browser->win); free(browser); + + menu_defaults(); + + return true; } void browser_close_committer(struct browser *browser) { - if (browser->committer) - committer_close(browser->committer); - SetPort(browser->win); + struct focusable *f; + + if (browser->committer) { + f = focusable_find(browser->committer->win); + if (f) + focusable_close(f); + } } void @@ -363,7 +393,7 @@ browser_show_commit(struct browser *browser, struct re } void -browser_revert_file(struct browser *browser) +browser_discard_changes(struct browser *browser) { char buf[256], filename[256]; Point pt = { 75, 100 }; @@ -435,6 +465,9 @@ browser_update_menu(struct browser *browser) TERec *diff; Cell cell = { 0, 0 }; + TextFont(systemFont); + TextSize(12); + HLock(browser->diff_te); diff = *(browser->diff_te); @@ -461,19 +494,21 @@ browser_update_menu(struct browser *browser) if (!browser->committer) { EnableItem(repo_menu, REPO_MENU_ADD_FILE_ID); - EnableItem(repo_menu, REPO_MENU_REVERT_FILE_ID); + EnableItem(repo_menu, REPO_MENU_DISCARD_CHANGES_ID); EnableItem(repo_menu, REPO_MENU_APPLY_PATCH_ID); } - if (LGetSelect(true, &cell, browser->commit_list)) + if (LGetSelect(true, &cell, browser->commit_list)) { EnableItem(commit_menu, COMMIT_MENU_EXPORT_ID); - else + } else { DisableItem(commit_menu, COMMIT_MENU_EXPORT_ID); + } } void -browser_update(struct browser *browser, EventRecord *event) +browser_update(struct focusable *focusable, EventRecord *event) { + struct browser *browser = (struct browser *)focusable->cookie; Str255 buf; Rect r; short what = -1; @@ -508,20 +543,22 @@ browser_update(struct browser *browser, EventRecord *e TextFont(applFont); TextSize(11); - browser_update_menu(browser); UpdtControl(browser->win, browser->win->visRgn); + browser_update_menu(browser); + break; } } void -browser_mouse_down(struct browser *browser, EventRecord *event) +browser_mouse_down(struct focusable *focusable, EventRecord *event) { Cell selected = { 0 }, now = { 0 }; Point p; ControlHandle control; Rect r; + struct browser *browser = (struct browser *)focusable->cookie; struct repo_commit *commit = NULL; short *selected_files = NULL, *now_selected_files = NULL; short nselected = 0, nnow_selected = 0; @@ -643,4 +680,45 @@ browser_mouse_down(struct browser *browser, EventRecor } break; } -} +} + +bool +browser_handle_menu(struct focusable *focusable, short menu, short item) +{ + struct browser *browser = (struct browser *)focusable->cookie; + + switch (menu) { + case EDIT_MENU_ID: + switch (item) { + case EDIT_MENU_COPY_ID: + TECopy(browser->diff_te); + return true; + case EDIT_MENU_SELECT_ALL_ID: + TESetSelect(0, 1024 * 32, browser->diff_te); + return true; + } + break; + case REPO_MENU_ID: + switch (item) { + case REPO_MENU_ADD_FILE_ID: + browser->state = BROWSER_STATE_ADD_FILE; + return true; + case REPO_MENU_DISCARD_CHANGES_ID: + browser->state = BROWSER_STATE_DISCARD_CHANGES; + return true; + case REPO_MENU_APPLY_PATCH_ID: + browser->state = BROWSER_STATE_APPLY_PATCH; + return true; + } + break; + case COMMIT_MENU_ID: + switch (item) { + case COMMIT_MENU_EXPORT_ID: + browser->state = BROWSER_STATE_EXPORT_PATCH; + return true; + } + break; + } + + return false; +} --- browser.h Wed Dec 15 16:15:17 2021 +++ browser.h Tue Aug 16 13:20:59 2022 @@ -28,10 +28,9 @@ enum { BROWSER_STATE_UPDATE_FILE_LIST, BROWSER_STATE_UPDATE_COMMIT_LIST, BROWSER_STATE_OPEN_COMMITTER, - BROWSER_STATE_COMMITTER_DO_DIFF, BROWSER_STATE_WAITING_FOR_COMMITTER, BROWSER_STATE_REMOVE_FILE, - BROWSER_STATE_REVERT_FILE, + BROWSER_STATE_DISCARD_CHANGES, BROWSER_STATE_EXPORT_PATCH, BROWSER_STATE_APPLY_PATCH }; @@ -49,20 +48,14 @@ struct browser { }; struct browser *browser_init(struct repo *repo); -void browser_close(struct browser *browser); void browser_update_titlebar(struct browser *browser); -void browser_close(struct browser *browser); -void browser_close_committer(struct browser *browser); -void browser_idle(struct browser *browser); -void browser_update_menu(struct browser *browser); -void browser_update(struct browser *browser, EventRecord *event); void browser_show_commit(struct browser *browser, struct repo_commit *commit); +void browser_close_committer(struct browser *browser); void browser_export_patch(struct browser *browser); short browser_is_all_files_selected(struct browser *browser); short browser_selected_file_ids(struct browser *browser, short **selected_files); -void browser_mouse_down(struct browser *browser, EventRecord *event); void browser_apply_patch(struct browser *browser); pascal void commit_list_ldef(short message, Boolean selected, --- committer.c Tue Jun 14 22:16:33 2022 +++ committer.c Tue Aug 16 13:40:20 2022 @@ -22,6 +22,7 @@ #include "browser.h" #include "committer.h" #include "diff.h" +#include "focusable.h" #include "repo.h" #include "settings.h" #include "tetab.h" @@ -33,15 +34,18 @@ long diff_format, diff_context, status = 0; char *ifdefname, *diffargs, *label[2], *ignore_pats; static short padding = 10; +struct committer *committer_diffing = NULL; -static DialogPtr committer_dialog = NULL; +bool committer_close(struct focusable *focusable); +void committer_idle(struct focusable *focusable, EventRecord *event); +void committer_update(struct focusable *focusable, EventRecord *event); +void committer_suspend(struct focusable *focusable); +void committer_resume(struct focusable *focusable); +void committer_key_down(struct focusable *focusable, EventRecord *event); +void committer_mouse_down(struct focusable *focusable, EventRecord *event); +bool committer_handle_menu(struct focusable *focusable, short menu, + short item); -/* so diff_* know what they're operating on */ -struct committer *cur_committer = NULL; - -/* the last-accessed TE to do cut/copy/paste/select operations on */ -static TEHandle committer_last_te = NULL; - void committer_generate_diff(struct committer *committer); void committer_update_menu(struct committer *committer); void committer_commit(struct committer *committer); @@ -54,6 +58,7 @@ committer_init(struct browser *browser) { Str255 title, filename; struct committer *committer; + struct focusable *focusable; Rect bounds = { 0 }, te_bounds = { 0 }; TextStyle style; short fh; @@ -140,15 +145,28 @@ committer_init(struct browser *browser) committer->commit_button = NewControl(committer->win, &bounds, "\pCommit", true, 1, 1, 1, pushButProc, 0L); - ShowWindow(committer->win); - committer_last_te = committer->log_te; - committer_update(committer, NULL); - DrawMenuBar(); + committer->last_te = committer->log_te; + + focusable = xmalloczero(sizeof(struct focusable)); + focusable->cookie = committer; + focusable->win = committer->win; + focusable->modal = true; + focusable->idle = committer_idle; + focusable->update = committer_update; + focusable->mouse_down = committer_mouse_down; + focusable->key_down = committer_key_down; + focusable->menu = committer_handle_menu; + focusable->close = committer_close; + focusable_add(focusable); + + committer->state = COMMITTER_STATE_DO_DIFF; } -void -committer_close(struct committer *committer) +bool +committer_close(struct focusable *focusable) { + struct committer *committer = (struct committer *)focusable->cookie; + committer->browser->committer = NULL; if (committer->diff_line != NULL) { @@ -162,26 +180,38 @@ committer_close(struct committer *committer) TEDispose(committer->log_te); TEDispose(committer->diff_te); DisposeWindow(committer->win); - SetPort(committer->browser->win); free(committer); - committer_last_te = NULL; + return true; } void -committer_idle(struct committer *committer) +committer_idle(struct focusable *focusable, EventRecord *event) { - if (committer_last_te == committer->log_te) - TEIdle(committer->log_te); + struct committer *committer = (struct committer *)focusable->cookie; + + switch (committer->state) { + case COMMITTER_STATE_IDLE: + if (committer->last_te == committer->log_te) + TEIdle(committer->log_te); + break; + case COMMITTER_STATE_DO_DIFF: + committer_generate_diff(committer); + committer->state = COMMITTER_STATE_IDLE; + break; + case COMMITTER_STATE_DO_COMMIT: + break; + } } void -committer_update(struct committer *committer, EventRecord *event) +committer_update(struct focusable *focusable, EventRecord *event) { Str255 buf; Rect r; short what = -1, len; + struct committer *committer = (struct committer *)focusable->cookie; if (event != NULL) what = event->what; @@ -230,22 +260,27 @@ committer_update(struct committer *committer, EventRec } void -committer_suspend(struct committer *committer) +committer_suspend(struct focusable *focusable) { + struct committer *committer = (struct committer *)focusable->cookie; + TEDeactivate(committer->log_te); TEDeactivate(committer->diff_te); } void -committer_resume(struct committer *committer) +committer_resume(struct focusable *focusable) { + struct committer *committer = (struct committer *)focusable->cookie; + TEActivate(committer->log_te); TEActivate(committer->diff_te); } void -committer_key_down(struct committer *committer, EventRecord *event) +committer_key_down(struct focusable *focusable, EventRecord *event) { + struct committer *committer = (struct committer *)focusable->cookie; char k; k = (event->message & charCodeMask); @@ -256,8 +291,9 @@ committer_key_down(struct committer *committer, EventR } void -committer_mouse_down(struct committer *committer, EventRecord *event) +committer_mouse_down(struct focusable *focusable, EventRecord *event) { + struct committer *committer = (struct committer *)focusable->cookie; Point p; ControlHandle control; Rect r; @@ -270,16 +306,15 @@ committer_mouse_down(struct committer *committer, Even if (PtInRect(p, &r)) { TEClick(p, ((event->modifiers & shiftKey) != 0), committer->diff_te); - committer_last_te = committer->diff_te; + committer->last_te = committer->diff_te; committer_update_menu(committer); return; } r = (*(committer->log_te))->viewRect; if (PtInRect(p, &r)) { - TEClick(p, ((event->modifiers & shiftKey) != 0), - committer->log_te); - committer_last_te = committer->log_te; + TEClick(p, ((event->modifiers & shiftKey) != 0), committer->log_te); + committer->last_te = committer->log_te; committer_update_menu(committer); return; } @@ -329,7 +364,7 @@ committer_update_menu(struct committer *committer) HLock(committer->diff_te); HLock(committer->log_te); - if (committer_last_te == committer->diff_te) { + if (committer->last_te == committer->diff_te) { DisableItem(edit_menu, EDIT_MENU_CUT_ID); if ((*(committer->diff_te))->selStart == (*(committer->diff_te))->selEnd) @@ -341,7 +376,7 @@ committer_update_menu(struct committer *committer) else DisableItem(edit_menu, EDIT_MENU_SELECT_ALL_ID); DisableItem(edit_menu, EDIT_MENU_PASTE_ID); - } else if (committer_last_te == committer->log_te) { + } else if (committer->last_te == committer->log_te) { if ((*(committer->log_te))->selStart == (*(committer->log_te))->selEnd) { DisableItem(edit_menu, EDIT_MENU_CUT_ID); @@ -363,9 +398,10 @@ committer_update_menu(struct committer *committer) HiliteControl(committer->commit_button, 255); DisableItem(repo_menu, REPO_MENU_ADD_FILE_ID); - DisableItem(repo_menu, REPO_MENU_REVERT_FILE_ID); + DisableItem(repo_menu, REPO_MENU_DISCARD_CHANGES_ID); DisableItem(repo_menu, REPO_MENU_APPLY_PATCH_ID); + DisableItem(commit_menu, COMMIT_MENU_EDIT_ID); DisableItem(commit_menu, COMMIT_MENU_EXPORT_ID); HUnlock(committer->log_te); @@ -396,21 +432,21 @@ committer_generate_diff(struct committer *committer) committer->diff_adds = 0; committer->diff_subs = 0; committer->ndiffed_files = 0; - committer->allow_commit = 0; + committer->allow_commit = false; committer->diffed_files = xmalloc(sizeof(struct diffed_file) * nselected_files); + committer->diff_too_big = false; - cur_committer = committer; - committer->diff_too_big = 0; - HLock(committer->diff_te); style.tsFont = monaco; style.tsSize = 9; - TESetStyle(doFont | doSize, &style, false, cur_committer->diff_te); + TESetStyle(doFont | doSize, &style, false, committer->diff_te); all_files = browser_is_all_files_selected(committer->browser); + committer_diffing = committer; + for (i = 0; i < nselected_files; i++) { file = repo_file_with_id(committer->browser->repo, selected_files[i]); @@ -432,18 +468,19 @@ committer_generate_diff(struct committer *committer) if (repo_diff_file(committer->browser->repo, file)) { committer->diffed_files[committer->ndiffed_files].flags |= DIFFED_FILE_TEXT; - committer->allow_commit = 1; + committer->allow_commit = true; } diff_finish(); committer->ndiffed_files++; } + committer_diffing = NULL; + HUnlock(committer->diff_te); InvalRect(&committer->win->portRect); UpdateScrollbarForTE(committer->diff_scroller, committer->diff_te, true); - cur_committer = NULL; progress(NULL); @@ -452,7 +489,7 @@ done_diffing: free(selected_files); SetCursor(&arrow); - if (committer->allow_commit == 0) { + if (!committer->allow_commit) { warnx("No changes detected"); browser_close_committer(committer->browser); } @@ -489,35 +526,41 @@ committer_commit(struct committer *committer) browser->state = BROWSER_STATE_UPDATE_COMMIT_LIST; } -void -committer_handle_menu(struct committer *committer, long menu_id) +bool +committer_handle_menu(struct focusable *focusable, short menu, short item) { - switch (HiWord(menu_id)) { + struct committer *committer = (struct committer *)focusable->cookie; + + switch (menu) { case EDIT_MENU_ID: - switch (LoWord(menu_id)) { + switch (item) { case EDIT_MENU_CUT_ID: - if (committer_last_te == committer->log_te) { + if (committer->last_te == committer->log_te) { TECut(committer->log_te); committer_update_menu(committer); } - break; + return true; case EDIT_MENU_COPY_ID: - TECopy(committer_last_te); + if (committer->last_te) + TECopy(committer->last_te); committer_update_menu(committer); - break; + return true; case EDIT_MENU_PASTE_ID: - if (committer_last_te == committer->log_te) { + if (committer->last_te == committer->log_te) { TEPaste(committer->log_te); committer_update_menu(committer); } - break; + return true; case EDIT_MENU_SELECT_ALL_ID: - TESetSelect(0, 1024 * 32, committer_last_te); + if (committer->last_te) + TESetSelect(0, 1024 * 32, committer->last_te); committer_update_menu(committer); - break; + return true; } break; } + + return false; } size_t @@ -526,48 +569,52 @@ diff_output(const char *format, ...) va_list argptr; size_t len, last_pos, last_line, i; - if (cur_committer->diff_line == NULL) { - cur_committer->diff_line = xNewHandle(DIFF_LINE_SIZE); - cur_committer->diff_line_pos = 0; - HLock(cur_committer->diff_line); + if (committer_diffing == NULL) + panic("diff_output without committer_diffing"); + + if (committer_diffing->diff_line == NULL) { + committer_diffing->diff_line = xNewHandle(DIFF_LINE_SIZE); + committer_diffing->diff_line_pos = 0; + HLock(committer_diffing->diff_line); } - last_pos = cur_committer->diff_line_pos; + last_pos = committer_diffing->diff_line_pos; last_line = 0; va_start(argptr, format); if (format[0] == '%' && format[1] == 'c' && format[2] == '\0') { /* avoid having to vsprintf just to append 1 character */ - (*(cur_committer->diff_line))[last_pos] = va_arg(argptr, int); + (*(committer_diffing->diff_line))[last_pos] = va_arg(argptr, int); len = 1; } else - len = vsprintf(*(cur_committer->diff_line) + last_pos, format, + len = vsprintf(*(committer_diffing->diff_line) + last_pos, format, argptr); va_end(argptr); - cur_committer->diff_line_pos += len; - if (cur_committer->diff_line_pos >= DIFF_LINE_SIZE) + committer_diffing->diff_line_pos += len; + if (committer_diffing->diff_line_pos >= DIFF_LINE_SIZE) err(1, "diff line overflow!"); - if (len == 1 && (*(cur_committer->diff_line))[last_pos] != '\r') + if (len == 1 && (*(committer_diffing->diff_line))[last_pos] != '\r') return 1; - for (i = last_pos; i < cur_committer->diff_line_pos; i++) { - if (((char *)*(cur_committer->diff_line))[i] == '\r') { - diff_append_line(*(cur_committer->diff_line) + last_line, + for (i = last_pos; i < committer_diffing->diff_line_pos; i++) { + if (((char *)*(committer_diffing->diff_line))[i] == '\r') { + diff_append_line(*(committer_diffing->diff_line) + last_line, i - last_line + 1, false); last_line = i + 1; } } - if (last_line == cur_committer->diff_line_pos) { - cur_committer->diff_line_pos = 0; + if (last_line == committer_diffing->diff_line_pos) { + committer_diffing->diff_line_pos = 0; } else if (last_line > 0) { - memmove(*(cur_committer->diff_line), *(cur_committer->diff_line) + - last_line, cur_committer->diff_line_pos - last_line); - cur_committer->diff_line_pos -= last_line; + memmove(*(committer_diffing->diff_line), + *(committer_diffing->diff_line) + last_line, + committer_diffing->diff_line_pos - last_line); + committer_diffing->diff_line_pos -= last_line; } return len; @@ -578,24 +625,27 @@ diff_append_line(char *str, size_t len, bool flush) { short tabsize; - if (cur_committer->diff_chunk == NULL) { - cur_committer->diff_chunk = xNewHandle(DIFF_CHUNK_SIZE); - cur_committer->diff_chunk_pos = 0; + if (committer_diffing == NULL) + panic("diff_append_line without committer_diffing"); + + if (committer_diffing->diff_chunk == NULL) { + committer_diffing->diff_chunk = xNewHandle(DIFF_CHUNK_SIZE); + committer_diffing->diff_chunk_pos = 0; } if (str[0] == '-' && str[1] != '-') - cur_committer->diff_subs++; + committer_diffing->diff_subs++; else if (str[0] == '+' && str[1] != '+') - cur_committer->diff_adds++; + committer_diffing->diff_adds++; - if (cur_committer->diff_chunk_pos + len >= DIFF_CHUNK_SIZE) + if (committer_diffing->diff_chunk_pos + len >= DIFF_CHUNK_SIZE) diff_chunk_write(); - HLock(cur_committer->diff_chunk); - memcpy(*(cur_committer->diff_chunk) + cur_committer->diff_chunk_pos, - str, len); - HUnlock(cur_committer->diff_chunk); - cur_committer->diff_chunk_pos += len; + HLock(committer_diffing->diff_chunk); + memcpy(*(committer_diffing->diff_chunk) + + committer_diffing->diff_chunk_pos, str, len); + HUnlock(committer_diffing->diff_chunk); + committer_diffing->diff_chunk_pos += len; if (flush) diff_chunk_write(); @@ -604,52 +654,60 @@ diff_append_line(char *str, size_t len, bool flush) void diff_chunk_write(void) { - HLock(cur_committer->diff_chunk); + if (committer_diffing == NULL) + panic("diff_chunk_write without committer_diffing"); + + HLock(committer_diffing->diff_chunk); - if (cur_committer->diff_te_len + cur_committer->diff_chunk_pos > - MAX_TEXTEDIT_SIZE) { - HUnlock((*(cur_committer->diff_te))->hText); - SetHandleSize((*(cur_committer->diff_te))->hText, - cur_committer->diff_te_len + cur_committer->diff_chunk_pos); + if (committer_diffing->diff_te_len + + committer_diffing->diff_chunk_pos > MAX_TEXTEDIT_SIZE) { + HUnlock((*(committer_diffing->diff_te))->hText); + SetHandleSize((*(committer_diffing->diff_te))->hText, + committer_diffing->diff_te_len + + committer_diffing->diff_chunk_pos); if (MemError()) err(1, "Out of memory! Can't expand diff TE by %lu bytes.", - cur_committer->diff_chunk_pos); - HLock((*(cur_committer->diff_te))->hText); - memcpy(*(*(cur_committer->diff_te))->hText + - cur_committer->diff_te_len, *(cur_committer->diff_chunk), - cur_committer->diff_chunk_pos); - HUnlock((*(cur_committer->diff_te))->hText); + committer_diffing->diff_chunk_pos); + HLock((*(committer_diffing->diff_te))->hText); + memcpy(*(*(committer_diffing->diff_te))->hText + + committer_diffing->diff_te_len, *(committer_diffing->diff_chunk), + committer_diffing->diff_chunk_pos); + HUnlock((*(committer_diffing->diff_te))->hText); } else { - TEStylInsert(*(cur_committer->diff_chunk), - cur_committer->diff_chunk_pos, 0, cur_committer->diff_te); + TEStylInsert(*(committer_diffing->diff_chunk), + committer_diffing->diff_chunk_pos, 0, + committer_diffing->diff_te); } - HUnlock(cur_committer->diff_chunk); + HUnlock(committer_diffing->diff_chunk); - cur_committer->diff_te_len += cur_committer->diff_chunk_pos; - cur_committer->diff_chunk_pos = 0; + committer_diffing->diff_te_len += committer_diffing->diff_chunk_pos; + committer_diffing->diff_chunk_pos = 0; } void diff_finish(void) { - if (cur_committer->diff_line != NULL) { - if (cur_committer->diff_line_pos) - diff_append_line(*(cur_committer->diff_line), - cur_committer->diff_line_pos, true); + if (committer_diffing == NULL) + panic("diff_finish without committer_diffing"); - DisposHandle(cur_committer->diff_line); - cur_committer->diff_line = NULL; + if (committer_diffing->diff_line != NULL) { + if (committer_diffing->diff_line_pos) + diff_append_line(*(committer_diffing->diff_line), + committer_diffing->diff_line_pos, true); + + DisposHandle(committer_diffing->diff_line); + committer_diffing->diff_line = NULL; } - if (cur_committer->diff_chunk != NULL) { - if (cur_committer->diff_chunk_pos) + if (committer_diffing->diff_chunk != NULL) { + if (committer_diffing->diff_chunk_pos) diff_chunk_write(); - DisposHandle(cur_committer->diff_chunk); - cur_committer->diff_chunk = NULL; + DisposHandle(committer_diffing->diff_chunk); + committer_diffing->diff_chunk = NULL; } - HUnlock((*(cur_committer->diff_te))->hText); - SetHandleSize((*(cur_committer->diff_te))->hText, - cur_committer->diff_te_len); - TECalText(cur_committer->diff_te); + HUnlock((*(committer_diffing->diff_te))->hText); + SetHandleSize((*(committer_diffing->diff_te))->hText, + committer_diffing->diff_te_len); + TECalText(committer_diffing->diff_te); } --- committer.h Sat Apr 16 13:08:55 2022 +++ committer.h Tue Aug 16 13:24:09 2022 @@ -18,12 +18,20 @@ #define __COMMITTER_H__ #include "browser.h" +#include "util.h" #define WAIT_DLOG_ID 128 +enum { + COMMITTER_STATE_IDLE, + COMMITTER_STATE_DO_DIFF, + COMMITTER_STATE_DO_COMMIT +}; + struct committer { struct browser *browser; WindowPtr win; + short state; TEHandle log_te; ControlHandle log_scroller; TEHandle diff_te; @@ -32,28 +40,21 @@ struct committer { ControlHandle commit_button; short ndiffed_files; struct diffed_file *diffed_files; - short allow_commit; + bool allow_commit; short diff_adds; short diff_subs; - short diff_too_big; + bool diff_too_big; #define DIFF_LINE_SIZE 512 Handle diff_line; size_t diff_line_pos; #define DIFF_CHUNK_SIZE (1024 * 4) Handle diff_chunk; size_t diff_chunk_pos; + TEHandle last_te; }; void committer_init(struct browser *browser); -void committer_close(struct committer *committer); -void committer_idle(struct committer *committer); -void committer_update(struct committer *committer, EventRecord *event); -void committer_suspend(struct committer *committer); -void committer_resume(struct committer *committer); -void committer_key_down(struct committer *committer, EventRecord *event); -void committer_mouse_down(struct committer *committer, EventRecord *event); void committer_generate_diff(struct committer *committer); -void committer_handle_menu(struct committer *committer, long menu_id); size_t diff_output(const char *format, ...); --- main.c Wed Jun 15 10:23:09 2022 +++ main.c Tue Aug 16 13:32:45 2022 @@ -20,18 +20,16 @@ #include "amend.h" #include "browser.h" #include "committer.h" +#include "focusable.h" #include "repo.h" #include "settings.h" #include "util.h" MenuHandle file_menu, edit_menu, repo_menu, commit_menu; Handle commit_list_ldef_h; -short quitting = 0; -struct browser *cur_browser = NULL; +bool quitting = false; void handle_menu(long menu_id); -void cur_browser_close(void); -void update_menu(void); int main(void) @@ -43,7 +41,9 @@ main(void) GrafPtr old_port; AppFile finder_file; struct repo *repo; - short event_in, i, did_initial_open = 0, finder_action, finder_count; + struct focusable *focusable; + short event_in, n, finder_action, finder_count; + bool did_initial_open = false; char key; InitGraf(&thePort); @@ -66,7 +66,7 @@ main(void) edit_menu = GetMHandle(EDIT_MENU_ID); repo_menu = GetMHandle(REPO_MENU_ID); commit_menu = GetMHandle(COMMIT_MENU_ID); - update_menu(); + menu_defaults(); DrawMenuBar(); /* dynamically patch list LDEF to point to our *_list_ldef funcs */ @@ -84,9 +84,11 @@ main(void) switch (event.what) { case nullEvent: - if (cur_browser) - browser_idle(cur_browser); - else if (!did_initial_open) { + for (n = 0; n < nfocusables; n++) { + if (focusables[n]->idle) + focusables[n]->idle(focusables[n], &event); + } + if (!did_initial_open) { if (finder_count) { GetAppFiles(1, &finder_file); ClrAppFiles(1); @@ -96,9 +98,9 @@ main(void) repo = repo_open(NULL); if (repo) - cur_browser = browser_init(repo); + browser_init(repo); - did_initial_open = 1; + did_initial_open = true; } break; case keyDown: @@ -106,13 +108,13 @@ main(void) key = event.message & charCodeMask; if ((event.modifiers & cmdKey) != 0) handle_menu(MenuKey(key)); - else if (cur_browser && cur_browser->committer && - FrontWindow() == cur_browser->committer->win) - committer_key_down(cur_browser->committer, &event); + else if ((focusable = focusable_focused()) && + focusable->key_down) + focusable->key_down(focusable, &event); break; - case mouseDown: + case mouseDown: event_in = FindWindow(event.where, &event_win); - + switch (event_in) { case inMenuBar: handle_menu(MenuSelect(event.where)); @@ -121,64 +123,62 @@ main(void) SystemClick(&event, event_win); break; case inDrag: - SelectWindow(event_win); - DragWindow(event_win, event.where, &screenBits.bounds); + if ((focusable = focusable_find(event_win)) != NULL) { + if (!focusable_show(focusable)) + break; + + DragWindow(event_win, event.where, &screenBits.bounds); + } break; case inGoAway: if (TrackGoAway(event_win, event.where)) { - if (cur_browser && event_win == cur_browser->win) - cur_browser_close(); - else if (cur_browser && cur_browser->committer && - event_win == cur_browser->committer->win) - browser_close_committer(cur_browser); + if ((focusable = focusable_find(event_win)) != NULL) + focusable_close(focusable); } break; case inContent: - if (event_win != FrontWindow()) - SelectWindow(event_win); - if (cur_browser && event_win == cur_browser->win) - browser_mouse_down(cur_browser, &event); - else if (cur_browser && cur_browser->committer && - event_win == cur_browser->committer->win) - committer_mouse_down(cur_browser->committer, &event); + if ((focusable = focusable_find(event_win)) != NULL) { + if (!focusable_show(focusable)) + break; + if (focusable->mouse_down) + focusable->mouse_down(focusable, &event); + } break; } break; case updateEvt: - if (cur_browser == NULL) - break; + event_win = (WindowPtr)event.message; GetPort(&old_port); - - event_win = (WindowPtr)event.message; SetPort(event_win); BeginUpdate(event_win); - if (event_win == cur_browser->win) - browser_update(cur_browser, &event); - else if (cur_browser->committer && - event_win == cur_browser->committer->win) - committer_update(cur_browser->committer, &event); + focusable = focusable_find(event_win); + if (focusable && focusable->update) + focusable->update(focusable, &event); EndUpdate(event_win); SetPort(old_port); break; + case activateEvt: + break; case app4Evt: if (HiWord(event.message) & (1 << 8)) { /* multifinder suspend/resume */ switch (event.message & (1 << 0)) { case 0: /* suspend */ - if (cur_browser && cur_browser->committer) - committer_suspend(cur_browser->committer); + for (n = 0; n < nfocusables; n++) { + if (focusables[n]->suspend) + focusables[n]->suspend(focusables[n], &event); + } break; case 1: /* resume */ - if (cur_browser && cur_browser->committer) { - SelectWindow(cur_browser->committer->win); - committer_resume(cur_browser->committer); - } else if (cur_browser) - SelectWindow(cur_browser->win); + for (n = 0; n < nfocusables; n++) { + if (focusables[n]->resume) + focusables[n]->resume(focusables[n], &event); + } break; } } @@ -190,19 +190,21 @@ main(void) } void -cur_browser_close(void) -{ - browser_close(cur_browser); - cur_browser = NULL; - update_menu(); -} - -void handle_menu(long menu_id) -{ - switch (HiWord(menu_id)) { +{ + struct focusable *focused; + short menu, item; + + menu = HiWord(menu_id); + item = LoWord(menu_id); + + if ((focused = focusable_focused()) && focused->menu && + focused->menu(focused, menu, item)) + goto handled; + + switch (menu) { case APPLE_MENU_ID: - switch (LoWord(menu_id)) { + switch (item) { case APPLE_MENU_ABOUT_ID: { VersRecHndl vers; char vers_s[255]; @@ -230,97 +232,46 @@ handle_menu(long menu_id) } break; case FILE_MENU_ID: - switch (LoWord(menu_id)) { + switch (item) { case FILE_MENU_NEW_ID: { struct repo *repo; - if (cur_browser) - cur_browser_close(); if ((repo = repo_create())) - cur_browser = browser_init(repo); + browser_init(repo); break; } case FILE_MENU_OPEN_ID: { struct repo *repo; - /* TODO: don't close unless we open a new repo */ - if (cur_browser) - cur_browser_close(); if ((repo = repo_open(NULL))) - cur_browser = browser_init(repo); + browser_init(repo); break; } case FILE_MENU_SETTINGS_ID: settings_edit(); break; case FILE_MENU_QUIT_ID: - if (cur_browser) { - browser_close(cur_browser); - cur_browser = NULL; - } - quitting = 1; + if (focusables_quit()) + quitting = true; break; } break; - case EDIT_MENU_ID: - if (cur_browser && cur_browser->committer) - committer_handle_menu(cur_browser->committer, menu_id); - else { - switch (LoWord(menu_id)) { - case EDIT_MENU_COPY_ID: - if (cur_browser) - TECopy(cur_browser->diff_te); - break; - case EDIT_MENU_SELECT_ALL_ID: - if (cur_browser) - TESetSelect(0, 1024 * 32, cur_browser->diff_te); - break; - } - } - break; - case REPO_MENU_ID: - switch (LoWord(menu_id)) { - case REPO_MENU_ADD_FILE_ID: - if (cur_browser) - cur_browser->state = BROWSER_STATE_ADD_FILE; - break; - case REPO_MENU_REVERT_FILE_ID: - if (cur_browser) - cur_browser->state = BROWSER_STATE_REVERT_FILE; - break; - case REPO_MENU_APPLY_PATCH_ID: - if (cur_browser) - cur_browser->state = BROWSER_STATE_APPLY_PATCH; - break; - } - break; - case COMMIT_MENU_ID: - switch (LoWord(menu_id)) { - case COMMIT_MENU_EXPORT_ID: - if (cur_browser) - cur_browser->state = BROWSER_STATE_EXPORT_PATCH; - break; - } - break; } - + +handled: HiliteMenu(0); } void -update_menu(void) +menu_defaults(void) { - if (cur_browser) { - browser_update_menu(cur_browser); - return; - } - DisableItem(edit_menu, EDIT_MENU_CUT_ID); DisableItem(edit_menu, EDIT_MENU_COPY_ID); DisableItem(edit_menu, EDIT_MENU_PASTE_ID); DisableItem(edit_menu, EDIT_MENU_SELECT_ALL_ID); DisableItem(repo_menu, REPO_MENU_ADD_FILE_ID); - DisableItem(repo_menu, REPO_MENU_REVERT_FILE_ID); + DisableItem(repo_menu, REPO_MENU_DISCARD_CHANGES_ID); DisableItem(repo_menu, REPO_MENU_APPLY_PATCH_ID); + DisableItem(commit_menu, COMMIT_MENU_EDIT_ID); DisableItem(commit_menu, COMMIT_MENU_EXPORT_ID); } --- repo.c Wed Jun 15 22:40:10 2022 +++ repo.c Tue Aug 16 14:38:02 2022 @@ -65,7 +65,7 @@ repo_open(AppFile *file) return NULL; } - progress("Verifying repository..."); + progress("Verifying repository structure..."); bile_verify(bile); progress("Reading repository..."); @@ -516,6 +516,8 @@ repo_add_file(struct repo *repo) } CtoPstr(reply.fName); + SetCursor(*(GetCursor(watchCursor))); + repo->nfiles++; repo->files = xrealloc(repo->files, repo->nfiles * sizeof(Ptr)); file = repo->files[repo->nfiles - 1] = @@ -530,6 +532,8 @@ repo_add_file(struct repo *repo) repo_file_update(repo, file); repo_sort_files(repo); + + SetCursor(&arrow); return file; }