From 9370a4763aacbb9278b5be9c92a2484e3652bc29 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Sun, 1 May 2022 21:39:33 +0800 Subject: [PATCH] Replace NS code that implemented font panels in a different way * doc/emacs/macos.texi (Mac / GNUstep Events): Document removal of `ns-change-font' event. The font panels are now implemented normally, via `x-select-font'. * lisp/term/common-win.el (x-setup-function-keys): Likewise. * lisp/term/ns-win.el (global-map, ns-popup-font-panel): Remove. (x-select-font, mouse-set-font, ns-input-font): (ns-input-fontsize): Remove. (ns-respond-to-change-font): Delete function. * src/nsfns.m (Fns_popup_font_panel): Delete function. (Fx_select_font): New function. (syms_of_nsfns): Update subrs. * src/nsterm.h (@interface EmacsView): * src/nsterm.m (ns_select_1): New function. (ns_select): Wrap around that instead. ([EmacsView changeFont:]): Exit nested event loop ([EmacsView showFontPanel:]): New function. --- doc/emacs/macos.texi | 8 ---- lisp/term/common-win.el | 1 - lisp/term/ns-win.el | 29 ----------- src/nsfns.m | 32 ++++++------- src/nsterm.h | 31 ++++++------ src/nsterm.m | 104 +++++++++++++++++++++++++++------------- 6 files changed, 103 insertions(+), 102 deletions(-) diff --git a/doc/emacs/macos.texi b/doc/emacs/macos.texi index ab143707fdd..37f48619d12 100644 --- a/doc/emacs/macos.texi +++ b/doc/emacs/macos.texi @@ -273,14 +273,6 @@ application is overriding the default behavior. The modifier keys listed above are defined by macOS and are unaffected by user changes to the modifiers in Emacs. -@item ns-change-font -This event occurs when the user selects a font in a Nextstep font -panel (which can be opened with @kbd{Cmd-t}). The default behavior is -to adjust the font of the selected frame -(@code{ns-respond-to-changefont}). The name and size of the selected -font are stored in the variables @code{ns-input-font} and -@code{ns-input-fontsize}, respectively. - @item ns-power-off This event occurs when the user logs out and Emacs is still running, or when ``Quit Emacs'' is chosen from the application menu. diff --git a/lisp/term/common-win.el b/lisp/term/common-win.el index b219014a73c..6f1e322aba5 100644 --- a/lisp/term/common-win.el +++ b/lisp/term/common-win.el @@ -65,7 +65,6 @@ (cons 4 'ns-drag-file) (cons 5 'ns-drag-color) (cons 6 'ns-drag-text) - (cons 7 'ns-change-font) (cons 8 'ns-open-file-line) ;;; (cons 9 'ns-insert-working-text) ;;; (cons 10 'ns-delete-working-text) diff --git a/lisp/term/ns-win.el b/lisp/term/ns-win.el index 065ca235b40..6a414d83f18 100644 --- a/lisp/term/ns-win.el +++ b/lisp/term/ns-win.el @@ -176,7 +176,6 @@ The properties returned may include `top', `left', `height', and `width'." (define-key global-map [ns-power-off] 'save-buffers-kill-emacs) (define-key global-map [ns-open-file] 'ns-find-file) (define-key global-map [ns-open-temp-file] [ns-open-file]) -(define-key global-map [ns-change-font] 'ns-respond-to-change-font) (define-key global-map [ns-open-file-line] 'ns-open-file-select-line) (define-key global-map [ns-spi-service-call] 'ns-spi-service-call) (define-key global-map [ns-new-frame] 'make-frame) @@ -623,34 +622,6 @@ If FRAME is nil, the change applies to the selected frame." ;; Needed for font listing functions under both backend and normal (setq scalable-fonts-allowed t) -;; Set to use font panel instead -(declare-function ns-popup-font-panel "nsfns.m" (&optional frame)) -(defalias 'x-select-font 'ns-popup-font-panel "Pop up the font panel. -This function has been overloaded in Nextstep.") -(defalias 'mouse-set-font 'ns-popup-font-panel "Pop up the font panel. -This function has been overloaded in Nextstep.") - -;; nsterm.m -(defvar ns-input-font) -(defvar ns-input-fontsize) - -(defun ns-respond-to-change-font () - "Set the font chosen in the font-picker panel. -Respond to changeFont: event, expecting ns-input-font and -ns-input-fontsize of new font." - (interactive) - (let ((face 'default)) - (set-face-attribute face t - :family ns-input-font - :height (* 10 ns-input-fontsize)) - (set-face-attribute face (selected-frame) - :family ns-input-font - :height (* 10 ns-input-fontsize)) - (let ((spec (list (list t (face-attr-construct 'default))))) - (put face 'customized-face spec) - (custom-push-theme 'theme-face face 'user 'set spec) - (put face 'face-modified nil)))) - ;; Default fontset for macOS. This is mainly here to show how a fontset ;; can be set up manually. Ordinarily, fontsets are auto-created whenever ;; a font is chosen by diff --git a/src/nsfns.m b/src/nsfns.m index 00d4a7d2bd2..b71a3d7376d 100644 --- a/src/nsfns.m +++ b/src/nsfns.m @@ -1593,26 +1593,22 @@ Some window managers may refuse to restack windows. */) } } -DEFUN ("ns-popup-font-panel", Fns_popup_font_panel, Sns_popup_font_panel, - 0, 1, "", - doc: /* Pop up the font panel. */) - (Lisp_Object frame) +DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0, + doc: /* Read a font using a Nextstep dialog. +Return a string describing the selected font. + +FRAME is the frame on which to pop up the font chooser. If omitted or +nil, it defaults to the selected frame. */) + (Lisp_Object frame, Lisp_Object ignored) { struct frame *f = decode_window_system_frame (frame); - id fm = [NSFontManager sharedFontManager]; - struct font *font = f->output_data.ns->font; - NSFont *nsfont; -#ifdef NS_IMPL_GNUSTEP - nsfont = ((struct nsfont_info *)font)->nsfont; -#endif -#ifdef NS_IMPL_COCOA - nsfont = (NSFont *) macfont_get_nsctfont (font); -#endif - [fm setSelectedFont: nsfont isMultiple: NO]; - [fm orderFrontFontPanel: NSApp]; - return Qnil; -} + Lisp_Object font = [FRAME_NS_VIEW (f) showFontPanel]; + if (NILP (font)) + quit (); + + return font; +} DEFUN ("ns-popup-color-panel", Fns_popup_color_panel, Sns_popup_color_panel, 0, 1, "", @@ -3299,7 +3295,7 @@ Default is t. */); defsubr (&Sns_emacs_info_panel); defsubr (&Sns_list_services); defsubr (&Sns_perform_service); - defsubr (&Sns_popup_font_panel); + defsubr (&Sx_select_font); defsubr (&Sns_popup_color_panel); defsubr (&Sx_show_tip); diff --git a/src/nsterm.h b/src/nsterm.h index 5b121ede98f..9d8a6f486fc 100644 --- a/src/nsterm.h +++ b/src/nsterm.h @@ -442,23 +442,25 @@ typedef id instancetype; #else @interface EmacsView : NSView #endif - { +{ #ifdef NS_IMPL_COCOA - char *old_title; - BOOL maximizing_resize; + char *old_title; + BOOL maximizing_resize; #endif - BOOL windowClosing; - NSString *workingText; - BOOL processingCompose; - int fs_state, fs_before_fs, next_maximized; - int maximized_width, maximized_height; - EmacsWindow *nonfs_window; - BOOL fs_is_native; + BOOL font_panel_active; + NSFont *font_panel_result; + BOOL windowClosing; + NSString *workingText; + BOOL processingCompose; + int fs_state, fs_before_fs, next_maximized; + int maximized_width, maximized_height; + EmacsWindow *nonfs_window; + BOOL fs_is_native; @public - struct frame *emacsframe; - int scrollbarsNeedingUpdate; - NSRect ns_userRect; - } + struct frame *emacsframe; + int scrollbarsNeedingUpdate; + NSRect ns_userRect; +} /* AppKit-side interface */ - (instancetype)menuDown: (id)sender; @@ -485,6 +487,7 @@ typedef id instancetype; #ifdef NS_IMPL_GNUSTEP - (void)windowDidMove: (id)sender; #endif +- (Lisp_Object) showFontPanel; - (int)fullscreenState; #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MIN_REQUIRED >= 101400 diff --git a/src/nsterm.m b/src/nsterm.m index 5d2e74ad56e..5e70e0d5665 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -4442,10 +4442,10 @@ ns_read_socket (struct terminal *terminal, struct input_event *hold_quit) } -int -ns_select (int nfds, fd_set *readfds, fd_set *writefds, - fd_set *exceptfds, struct timespec *timeout, - sigset_t *sigmask) +static int +ns_select_1 (int nfds, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds, struct timespec *timeout, + sigset_t *sigmask, BOOL run_loop_only) /* -------------------------------------------------------------------------- Replacement for select, checking for events -------------------------------------------------------------------------- */ @@ -4461,7 +4461,7 @@ ns_select (int nfds, fd_set *readfds, fd_set *writefds, check_native_fs (); #endif - if (hold_event_q.nr > 0) + if (hold_event_q.nr > 0 && !run_loop_only) { /* We already have events pending. */ raise (SIGIO); @@ -4479,12 +4479,12 @@ ns_select (int nfds, fd_set *readfds, fd_set *writefds, if (NSApp == nil || ![NSThread isMainThread] || (timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0)) - return thread_select(pselect, nfds, readfds, writefds, - exceptfds, timeout, sigmask); + return thread_select (pselect, nfds, readfds, writefds, + exceptfds, timeout, sigmask); else { struct timespec t = {0, 0}; - thread_select(pselect, 0, NULL, NULL, NULL, &t, sigmask); + thread_select (pselect, 0, NULL, NULL, NULL, &t, sigmask); } /* FIXME: This draining of outerpool causes a crash when a buffer @@ -4602,6 +4602,15 @@ ns_select (int nfds, fd_set *readfds, fd_set *writefds, return result; } +int +ns_select (int nfds, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds, struct timespec *timeout, + sigset_t *sigmask) +{ + return ns_select_1 (nfds, readfds, writefds, exceptfds, + timeout, sigmask, NO); +} + #ifdef HAVE_PTHREAD void ns_run_loop_break (void) @@ -6082,40 +6091,72 @@ not_in_argv (NSString *arg) /* Called on font panel selection. */ -- (void)changeFont: (id)sender +- (void) changeFont: (id) sender { - struct face *face = FACE_FROM_ID (emacsframe, DEFAULT_FACE_ID); - struct font *font = face->font; - id newFont; - CGFloat size; + struct font *font = FRAME_OUTPUT_DATA (emacsframe)->font; NSFont *nsfont; - struct input_event ie; - - NSTRACE ("[EmacsView changeFont:]"); - EVENT_INIT (ie); #ifdef NS_IMPL_GNUSTEP - nsfont = ((struct nsfont_info *)font)->nsfont; -#endif -#ifdef NS_IMPL_COCOA + nsfont = ((struct nsfont_info *) font)->nsfont; +#else nsfont = (NSFont *) macfont_get_nsctfont (font); #endif - if ((newFont = [sender convertFont: nsfont])) - { - ie.kind = NS_NONKEY_EVENT; - ie.modifiers = 0; - ie.code = KEY_NS_CHANGE_FONT; - XSETFRAME (ie.frame_or_window, emacsframe); + if (!font_panel_active) + return; - size = [newFont pointSize]; - ns_input_fontsize = make_fixnum (lrint (size)); - ns_input_font = [[newFont familyName] lispString]; + if (font_panel_result) + [font_panel_result release]; - kbd_buffer_store_event (&ie); - } + font_panel_result = (NSFont *) [sender convertFont: nsfont]; + + if (font_panel_result) + [font_panel_result retain]; + + font_panel_active = NO; + [NSApp stop: self]; } +- (Lisp_Object) showFontPanel +{ + id fm = [NSFontManager sharedFontManager]; + struct font *font = FRAME_OUTPUT_DATA (emacsframe)->font; + NSFont *nsfont, *result; + struct timespec timeout; + +#ifdef NS_IMPL_GNUSTEP + nsfont = ((struct nsfont_info *) font)->nsfont; +#else + nsfont = (NSFont *) macfont_get_nsctfont (font); +#endif + + [fm setSelectedFont: nsfont isMultiple: NO]; + [fm orderFrontFontPanel: NSApp]; + + font_panel_active = YES; + timeout = make_timespec (0, 100000000); + + block_input (); + while (font_panel_active + && [[fm fontPanel: YES] isVisible]) + ns_select_1 (0, NULL, NULL, NULL, &timeout, NULL, YES); + unblock_input (); + + if (font_panel_result) + [font_panel_result autorelease]; + + result = font_panel_result; + font_panel_result = nil; + + [[fm fontPanel: YES] setIsVisible: NO]; + font_panel_active = NO; + + /* TODO: return a font spec instead of a string. */ + if (result) + return [[result familyName] lispString]; + + return Qnil; +} - (BOOL)acceptsFirstResponder { @@ -6123,7 +6164,6 @@ not_in_argv (NSString *arg) return YES; } - - (void)resetCursorRects { NSRect visible = [self visibleRect]; -- 2.39.2