Skip to content

Commit

Permalink
Cleanup & unify window creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr committed Sep 13, 2019
1 parent 9caed0e commit 1c061b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
50 changes: 32 additions & 18 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

//! Window building and app lifecycle.

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
//use std::rc::Arc;

use crate::shell::window::WindowHandle;
use crate::shell::{init, runloop, Error as PlatformError, WindowBuilder};
use crate::win_handler::AppState;
use crate::window::{Window, WindowId};
Expand Down Expand Up @@ -55,24 +57,11 @@ impl<T: Data + 'static> AppLauncher<T> {
pub fn launch(self, data: T) -> Result<(), PlatformError> {
init();
let mut main_loop = runloop::RunLoop::new();
let env = theme::init();
let state = AppState::new(data, env);

let state = AppState::new(data, theme::init());
for window in self.windows {
let WindowDesc {
root_builder,
title,
..
} = window;

let id = WindowId::new();
let handler = DruidHandler::new_shared(state.clone(), id);
let title = title.unwrap_or(LocalizedString::new("app-name"));
let root = root_builder();
state.borrow_mut().add_window(id, Window::new(root, title));

let mut builder = WindowBuilder::new();
builder.set_handler(Box::new(handler));
let window = builder.build()?;
for desc in self.windows {
let window = desc.build_native(&state)?;
window.show();
}

Expand Down Expand Up @@ -110,4 +99,29 @@ impl<T: Data + 'static> WindowDesc<T> {
self.title = Some(title);
self
}

/// Attempt to create a platform window from this `WindowDesc`.
pub(crate) fn build_native(
&self,
state: &Rc<RefCell<AppState<T>>>,
) -> Result<WindowHandle, PlatformError> {
let mut title = self
.title
.clone()
.unwrap_or(LocalizedString::new("app-name"));
title.resolve(&state.borrow().data, &state.borrow().env);

let id = WindowId::new();
let handler = DruidHandler::new_shared(state.clone(), id);

let mut builder = WindowBuilder::new();
builder.set_handler(Box::new(handler));
builder.set_title(title.localized_str());
let root = (self.root_builder)();
state.borrow_mut().add_window(id, Window::new(root, title));

Ok(WindowHandle {
inner: builder.build()?,
})
}
}
29 changes: 6 additions & 23 deletions src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ use log::{error, warn};
use crate::kurbo::{Size, Vec2};
use crate::piet::{Color, Piet, RenderContext};
use crate::shell::window::{Cursor, WinCtx, WinHandler, WindowHandle};
use crate::shell::WindowBuilder;

use crate::window::Window;
use crate::{
BaseState, Command, Data, Env, Event, EventCtx, KeyEvent, KeyModifiers, LayoutCtx,
LocalizedString, MouseEvent, PaintCtx, Selector, TimerToken, UpdateCtx, WheelEvent, WindowDesc,
WindowId,
BaseState, Command, Data, Env, Event, EventCtx, KeyEvent, KeyModifiers, LayoutCtx, MouseEvent,
PaintCtx, Selector, TimerToken, UpdateCtx, WheelEvent, WindowDesc, WindowId,
};

// TODO: this should come from the theme.
Expand Down Expand Up @@ -363,33 +361,18 @@ impl<T: Data + 'static> DruidHandler<T> {
}

fn new_window(&mut self, cmd: Command) {
let WindowDesc {
root_builder,
title,
..
} = match cmd.get_object::<WindowDesc<T>>() {
let desc = match cmd.get_object::<WindowDesc<T>>() {
Some(wd) => wd,
None => {
warn!("new_window command is missing window description");
return;
}
};

let id = WindowId::new();
let handler = DruidHandler::new_shared(self.app_state.clone(), id);
let title = title
.to_owned()
.unwrap_or(LocalizedString::new("app-name-exclaim"));
let root = root_builder();
self.app_state
.borrow_mut()
.add_window(id, Window::new(root, title));
let mut builder = WindowBuilder::new();
builder.set_handler(Box::new(handler));
let window = match builder.build() {
Ok(w) => w,
let window = match desc.build_native(&self.app_state) {
Ok(win) => win,
Err(e) => {
error!("window creation failed: {:?}", e);
error!("failed to create window: '{:?}'", e);
return;
}
};
Expand Down

0 comments on commit 1c061b2

Please sign in to comment.