Skip to content

Commit 76103b5

Browse files
committed
Merge branch 'master' of github.com:compio-rs/winio
2 parents 74e847b + 41db1f8 commit 76103b5

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/ui/mac/text_box.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
use objc2::{
2-
DeclaredClass, MainThreadOnly, define_class, msg_send,
2+
AnyThread, DeclaredClass, MainThreadOnly, define_class, msg_send,
33
rc::{Allocated, Retained},
44
runtime::ProtocolObject,
55
};
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+
};
814

915
use crate::{
1016
AsWindow, HAlign, Point, Size,
11-
ui::{Callback, Widget, from_nsstring},
17+
ui::{Callback, Widget, from_cgsize, from_nsstring},
1218
};
1319

1420
#[derive(Debug)]
@@ -59,7 +65,23 @@ impl TextBox {
5965
}
6066

6167
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+
}
6385
}
6486

6587
pub fn loc(&self) -> Point {

src/ui/mac/window.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use objc2::{
66
runtime::ProtocolObject,
77
};
88
use objc2_app_kit::{
9-
NSBackingStoreType, NSControl, NSScreen, NSView, NSWindow, NSWindowDelegate, NSWindowStyleMask,
9+
NSBackingStoreType, NSControl, NSScreen, NSView, NSWindow, NSWindowDelegate,
10+
NSWindowOrderingMode, NSWindowStyleMask,
1011
};
1112
use objc2_foundation::{
1213
MainThreadMarker, NSNotification, NSObject, NSObjectProtocol, NSPoint, NSRect, NSSize, NSString,
@@ -44,7 +45,10 @@ impl Window {
4445
};
4546

4647
if let Some(parent) = parent {
47-
wnd.setParentWindow(Some(&parent.as_window().as_raw_window()));
48+
parent
49+
.as_window()
50+
.as_raw_window()
51+
.addChildWindow_ordered(&wnd, NSWindowOrderingMode::Above);
4852
}
4953

5054
let delegate = WindowDelegate::new(mtm);
@@ -217,8 +221,7 @@ impl Widget {
217221
pub fn preferred_size(&self) -> Size {
218222
unsafe {
219223
from_cgsize(
220-
Retained::cast_unchecked::<NSControl>(self.view.clone())
221-
.sizeThatFits(NSSize::new(f64::MAX, f64::MAX)),
224+
Retained::cast_unchecked::<NSControl>(self.view.clone()).sizeThatFits(NSSize::ZERO),
222225
)
223226
}
224227
}

0 commit comments

Comments
 (0)