From d5f8b0d6e56542e88a55202699f8c779cfe342d4 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Wed, 31 Aug 2022 17:02:44 -0400 Subject: [PATCH 1/3] feat: support for monospace system font Provides support for the `monospacedSystemFontOfSize` method from `NSFont`. Refer: https://developer.apple.com/documentation/appkit/nsfont/3042659-monospacedsystemfontofsize?language=objc --- src/text/font.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/text/font.rs b/src/text/font.rs index 17ef473..2a38d39 100644 --- a/src/text/font.rs +++ b/src/text/font.rs @@ -40,6 +40,14 @@ impl Font { Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), boldSystemFontOfSize: size]) }) } + + /// Creates and returns a monospace system font at the specified size and weight + pub fn monospace(size: f64, weight: f64) -> Self { + let size = size as CGFloat; + let weight = weight as CGFloat; + + Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), monospacedSystemFontOfSize: size weight: weight]) }) + } } impl Deref for Font { From 8c51b953d3992567b943b7c03c2a9ccea94aca46 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Sat, 15 Oct 2022 20:24:42 -0300 Subject: [PATCH 2/3] fix: provide version fallback --- src/text/font.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/text/font.rs b/src/text/font.rs index 2a38d39..a38ff6f 100644 --- a/src/text/font.rs +++ b/src/text/font.rs @@ -9,6 +9,7 @@ use objc::{class, msg_send, sel, sel_impl}; use objc_id::ShareId; use crate::foundation::{id, nil, NSArray, NSString, NO, YES}; +use crate::utils::os; /// A `Font` can be constructed and applied to supported controls to control things like text /// appearance and size. @@ -42,11 +43,22 @@ impl Font { } /// Creates and returns a monospace system font at the specified size and weight + /// + /// # Support + /// + /// The `monospace` font feature is available from version `10.15`. + /// + /// If the current system is using an older version the `monospacedSystemFontOfSize` + /// option will be omitted. pub fn monospace(size: f64, weight: f64) -> Self { - let size = size as CGFloat; - let weight = weight as CGFloat; + if os::is_minimum_semversion(10, 15, 0) { + let size = size as CGFloat; + let weight = weight as CGFloat; - Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), monospacedSystemFontOfSize: size weight: weight]) }) + Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), monospacedSystemFontOfSize: size weight: weight]) }) + } else { + Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), weight: weight]) }) + } } } From 2f54ae59af8ce2c0f62dd776f2fede5d865d4519 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Sun, 16 Oct 2022 22:20:40 -0300 Subject: [PATCH 3/3] fix: use `systemFontOfSize` as fallback for monospace --- src/text/font.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/text/font.rs b/src/text/font.rs index a38ff6f..d0151fe 100644 --- a/src/text/font.rs +++ b/src/text/font.rs @@ -51,13 +51,13 @@ impl Font { /// If the current system is using an older version the `monospacedSystemFontOfSize` /// option will be omitted. pub fn monospace(size: f64, weight: f64) -> Self { - if os::is_minimum_semversion(10, 15, 0) { - let size = size as CGFloat; - let weight = weight as CGFloat; + let size = size as CGFloat; + let weight = weight as CGFloat; + if os::is_minimum_semversion(10, 15, 0) { Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), monospacedSystemFontOfSize: size weight: weight]) }) } else { - Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), weight: weight]) }) + Font(unsafe { ShareId::from_ptr(msg_send![class!(NSFont), systemFontOfSize: size weight: weight ]) }) } } }