|
1 | 1 | use objc2::{ |
2 | | - DeclaredClass, MainThreadOnly, define_class, msg_send, |
| 2 | + AnyThread, DeclaredClass, MainThreadOnly, define_class, msg_send, |
3 | 3 | rc::{Allocated, Retained}, |
4 | 4 | runtime::ProtocolObject, |
5 | 5 | }; |
6 | | -use objc2_app_kit::{NSTextAlignment, NSTextDelegate, NSTextView, NSTextViewDelegate}; |
7 | | -use objc2_foundation::{MainThreadMarker, NSNotification, NSObject, NSObjectProtocol, NSString}; |
| 6 | +use objc2_app_kit::{ |
| 7 | + NSAttributedStringNSStringDrawing, NSFontAttributeName, NSTextAlignment, NSTextDelegate, |
| 8 | + NSTextView, NSTextViewDelegate, |
| 9 | +}; |
| 10 | +use objc2_foundation::{ |
| 11 | + MainThreadMarker, NSAttributedString, NSDictionary, NSNotification, NSObject, NSObjectProtocol, |
| 12 | + NSString, |
| 13 | +}; |
8 | 14 |
|
9 | 15 | use crate::{ |
10 | 16 | AsWindow, HAlign, Point, Size, |
11 | | - ui::{Callback, Widget, from_nsstring}, |
| 17 | + ui::{Callback, Widget, from_cgsize, from_nsstring}, |
12 | 18 | }; |
13 | 19 |
|
14 | 20 | #[derive(Debug)] |
@@ -59,7 +65,23 @@ impl TextBox { |
59 | 65 | } |
60 | 66 |
|
61 | 67 | pub fn preferred_size(&self) -> Size { |
62 | | - self.handle.preferred_size() |
| 68 | + unsafe { |
| 69 | + let font = self.text_view.font(); |
| 70 | + let text = NSAttributedString::initWithString_attributes( |
| 71 | + NSAttributedString::alloc(), |
| 72 | + &self.text_view.string(), |
| 73 | + if let Some(font) = font { |
| 74 | + Some(NSDictionary::from_slices( |
| 75 | + &[NSFontAttributeName], |
| 76 | + &[font.as_ref()], |
| 77 | + )) |
| 78 | + } else { |
| 79 | + None |
| 80 | + } |
| 81 | + .as_deref(), |
| 82 | + ); |
| 83 | + from_cgsize(text.size()) |
| 84 | + } |
63 | 85 | } |
64 | 86 |
|
65 | 87 | pub fn loc(&self) -> Point { |
|
0 commit comments