Skip to content

Commit 76f614a

Browse files
committed
Fix various TODOs
1 parent 8700b51 commit 76f614a

10 files changed

Lines changed: 68 additions & 28 deletions

File tree

src/platform/macos/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl WindowContext {
6767
return Ok(());
6868
}
6969

70-
BaseviewView::resize(view, size);
70+
BaseviewView::resize(view, size, true);
7171

7272
Ok(())
7373
}

src/platform/macos/view.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl BaseviewView {
159159
}
160160
}
161161

162-
pub fn resize(this: ViewRef<Self>, size: Size) {
162+
pub fn resize(this: ViewRef<Self>, size: Size, notify_host: bool) {
163163
let size = size.to_logical::<f64>(this.view.backing_scale_factor());
164164
// NOTE: macOS gives you a personal rave if you pass in fractional pixels here. Even
165165
// though the size is in fractional pixels.
@@ -182,7 +182,7 @@ impl BaseviewView {
182182
}
183183
}
184184

185-
Self::view_did_change_backing_properties(this);
185+
Self::view_did_change_backing_properties(this, notify_host);
186186
}
187187

188188
/// Trigger the event immediately and return the event status.
@@ -229,7 +229,7 @@ impl ViewImpl for BaseviewView {
229229
true
230230
}
231231

232-
fn view_did_change_backing_properties(this: ViewRef<Self>) {
232+
fn view_did_change_backing_properties(this: ViewRef<Self>, notify_host: bool) {
233233
let current_size = this.view.size();
234234
let current_scale_factor = this.view.backing_scale_factor();
235235

@@ -247,13 +247,17 @@ impl ViewImpl for BaseviewView {
247247
if let Some(Err(e)) = result {
248248
warn!("Window Handler failed to resize: {}", e);
249249
this.state.size.set(previous);
250-
Self::resize(this, previous.into())
250+
251+
Self::resize(this, previous.into(), false);
252+
return;
251253
}
252254

253-
// TODO: only if not coming from host
254-
if let Err(e) = this.host.request_resize(new_size) {
255-
warn!("Host failed to resize parent view: {}", e);
256-
Self::resize(this, previous.into()) // TODO: break error loop?
255+
if notify_host {
256+
if let Err(e) = this.host.request_resize(new_size) {
257+
warn!("Host failed to resize parent view: {}", e);
258+
259+
Self::resize(this, previous.into(), false);
260+
}
257261
}
258262
}
259263
}

src/platform/macos/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl WindowHandle {
108108
let Some(view) = self.view.load() else { return Ok(()) };
109109
let Some(view) = view.inner_ref() else { return Ok(()) };
110110

111-
BaseviewView::resize(view, size);
111+
BaseviewView::resize(view, size, false);
112112

113113
Ok(())
114114
}

src/platform/x11/error.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ impl Display for FatalError {
2222
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2323
match self {
2424
FatalError::Connection(e) => e.fmt(f),
25-
// TODO: better errors
26-
FatalError::SendMainThread => f.write_str("SendMainThread"),
25+
FatalError::SendMainThread => {
26+
f.write_str("Failed to send callback from X11 thread to main thread")
27+
}
2728
}
2829
}
2930
}
@@ -51,7 +52,7 @@ pub enum Error {
5152
Connect(ConnectError),
5253
DisplayOpenFailed(DisplayOpenFailedError),
5354
Handler(HandlerError),
54-
Channel(RecvError),
55+
MainThreadRecvResult,
5556
Calloop(calloop::Error),
5657
RequestFromMainThreadFailed(RequestFailed),
5758
#[cfg(feature = "opengl")]
@@ -65,7 +66,26 @@ impl Display for Error {
6566
match self {
6667
Error::Io(e) => e.fmt(f),
6768
Self::IdsExhausted => f.write_str("X11 IDs have been exhausted"),
68-
_ => todo!(),
69+
Error::CreationFailed(e) => write!(f, "Failed to create window: {e}"),
70+
Error::Run(e) => write!(f, "Error in running X11 thread: {e}"),
71+
Error::DylibOpen(e) => e.fmt(f),
72+
Error::InitThreadsFailed(e) => e.fmt(f),
73+
Error::X11(e) => write!(f, "X server replied with error: {e:?}"),
74+
Error::Connection(e) => e.fmt(f),
75+
Error::Parse(e) => e.fmt(f),
76+
Error::GetProperty(e) => e.fmt(f),
77+
Error::Connect(e) => e.fmt(f),
78+
Error::DisplayOpenFailed(e) => e.fmt(f),
79+
Error::Handler(e) => e.fmt(f),
80+
Error::MainThreadRecvResult => {
81+
f.write_str("Failed to receive Window creation response from X11 thread: channel was closed unexpectedly")
82+
}
83+
Error::Calloop(e) => e.fmt(f),
84+
Error::RequestFromMainThreadFailed(e) => e.fmt(f),
85+
#[cfg(feature = "opengl")]
86+
Error::XLib(e) => e.fmt(f),
87+
#[cfg(feature = "opengl")]
88+
Error::Gl(e) => e.fmt(f),
6989
}
7090
}
7191
}
@@ -74,7 +94,6 @@ impl std::error::Error for Error {
7494
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
7595
match self {
7696
Error::Io(e) => Some(e),
77-
Error::Channel(e) => Some(e),
7897
Error::DylibOpen(e) => Some(e),
7998
Error::Connect(e) => Some(e),
8099
Error::Handler(e) => Some(e.source()),
@@ -133,12 +152,6 @@ impl From<calloop::Error> for Error {
133152
}
134153
}
135154

136-
impl From<RecvError> for Error {
137-
fn from(value: RecvError) -> Self {
138-
Self::Channel(value)
139-
}
140-
}
141-
142155
impl From<RequestFailed> for Error {
143156
fn from(value: RequestFailed) -> Self {
144157
Self::RequestFromMainThreadFailed(value)

src/platform/x11/event_loop.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ impl EventLoop {
134134
self.window.store_size(previous);
135135
self.window.xcb_window.resize(previous.cast())?.check_warn();
136136
} else {
137-
// TODO: only if this wansn't the result of a host request
137+
// Host requests use resize_immediately, which stops the previous == new_size condition
138+
// So if we're here, it's guaranteed not to be from a host request
139+
138140
if let Some(host) = self.main_thread.as_mut() {
139141
host.send(HostCallback::Resized {
140142
new_size,

src/platform/x11/gl.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ pub enum CreationFailedError {
2020
OpenError(OpenError),
2121
}
2222

23+
impl Display for CreationFailedError {
24+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25+
match self {
26+
CreationFailedError::NoValidFBConfig => {
27+
f.write_str("Could not find a valid Framebuffer configuration")
28+
}
29+
CreationFailedError::NoVisual => {
30+
f.write_str("Could not find a matching visual configuration")
31+
}
32+
CreationFailedError::GetProcAddressFailed => f.write_str("GetProcAddress failed"),
33+
CreationFailedError::MakeCurrentFailed => f.write_str("MakeCurrent failed"),
34+
CreationFailedError::ContextCreationFailed => f.write_str("Faile to create GL context"),
35+
CreationFailedError::X11Error(e) => e.fmt(f),
36+
CreationFailedError::OpenError(e) => e.fmt(f),
37+
}
38+
}
39+
}
40+
2341
pub type GlContext = Rc<GlContextInner>;
2442

2543
pub struct GlContextInner {

src/platform/x11/window_thread.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ impl WindowResultSender {
298298
struct WindowResultReceiver(mpsc::Receiver<WindowOpenResult>);
299299
impl WindowResultReceiver {
300300
pub fn receive(self) -> Result<LoopSignal> {
301-
let result = self.0.recv()?;
301+
let result = self.0.recv().map_err(|_| Error::MainThreadRecvResult)?;
302+
302303
match result {
303304
WindowOpenResult::Error(e) => Err(Error::CreationFailed(e)),
304305
WindowOpenResult::Success { loop_signal } => Ok(loop_signal),

src/platform/x11/xcb_window.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::platform::x11::error::CookieExt;
12
use crate::platform::x11::visual_info::WindowVisualConfig;
23
use crate::platform::X11Connection;
34
use dpi::PhysicalSize;
@@ -111,8 +112,9 @@ impl XcbWindow {
111112

112113
impl Drop for XcbWindow {
113114
fn drop(&mut self) {
114-
// TODO: log error
115-
let Ok(cookie) = self.connection.conn.destroy_window(self.window_id.get()) else { return };
116-
let _ = cookie.check();
115+
match self.connection.conn.destroy_window(self.window_id.get()) {
116+
Err(e) => crate::warn!("Failed to send request to destroy X window: {}", e),
117+
Ok(cookie) => cookie.check_warn(),
118+
}
117119
}
118120
}

src/wrappers/appkit/view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub trait ViewImpl: Sized {
152152
fn become_first_responder(this: ViewRef<Self>) -> bool;
153153
fn resign_first_responder(this: ViewRef<Self>) -> bool;
154154
fn window_should_close(this: ViewRef<Self>) -> bool;
155-
fn view_did_change_backing_properties(this: ViewRef<Self>);
155+
fn view_did_change_backing_properties(this: ViewRef<Self>, from_host: bool);
156156
fn hit_test(this: ViewRef<'_, Self>, point: NSPoint) -> Option<&NSView>;
157157
fn view_will_move_to_window(this: ViewRef<Self>, new_window: Option<&NSWindow>);
158158
fn update_tracking_areas(this: ViewRef<Self>);

src/wrappers/appkit/view/implementation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ extern "C-unwind" fn view_did_change_backing_properties<V: ViewImpl>(
187187
this: &View<V>, _: Sel, _: &AnyObject,
188188
) {
189189
let Some(inner) = this.inner_ref() else { return };
190-
V::view_did_change_backing_properties(inner);
190+
V::view_did_change_backing_properties(inner, true);
191191
}
192192

193193
extern "C-unwind" fn hit_test<V: ViewImpl>(

0 commit comments

Comments
 (0)