diff --git a/src/main.rs b/src/main.rs index f563caa..63831a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -398,11 +398,7 @@ impl App { self.update_nav_bar_active(); let title = match self.active_tab() { - Some(tab) => { - // Hack to ensure redraw on changing tabs - tab.editor.lock().unwrap().buffer_mut().set_redraw(true); - tab.title() - } + Some(tab) => tab.title(), None => format!("No Open File"), }; @@ -836,8 +832,6 @@ impl Application for App { Some(tab) => { // Close context menu tab.context_menu = None; - // Hack to ensure editor redraws - tab.editor.lock().unwrap().buffer_mut().set_redraw(true); // Run action's message return self.update(action.message()); } @@ -849,8 +843,6 @@ impl Application for App { Some(tab) => { // Update context menu tab.context_menu = position_opt; - // Hack to ensure editor redraws - tab.editor.lock().unwrap().buffer_mut().set_redraw(true); } None => {} } @@ -874,10 +866,6 @@ impl Application for App { self.core.window.show_context = true; } self.set_context_title(context_page.title()); - - // Hack to ensure tab redraws. - //TODO: tab does not redraw when using Close button! - return self.update_tab(); } Message::ToggleWordWrap => { self.config.word_wrap = !self.config.word_wrap; diff --git a/src/text_box.rs b/src/text_box.rs index d1f392c..c9c189c 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -328,7 +328,8 @@ where // Shape and layout as needed editor.shape_as_needed(); - if editor.buffer().redraw() { + let mut handle_opt = state.handle_opt.lock().unwrap(); + if editor.buffer().redraw() || handle_opt.is_none() { // Draw to pixel buffer let mut pixels = vec![0; image_w as usize * image_h as usize * 4]; { @@ -378,26 +379,30 @@ where editor.buffer_mut().set_redraw(false); state.scale_factor.set(scale_factor); - *state.handle.lock().unwrap() = - image::Handle::from_pixels(image_w as u32, image_h as u32, pixels); + *handle_opt = Some(image::Handle::from_pixels( + image_w as u32, + image_h as u32, + pixels, + )); } - let handle = state.handle.lock().unwrap().clone(); let image_position = layout.position() + [self.padding.left as f32, self.padding.top as f32].into(); - let image_size = image::Renderer::dimensions(renderer, &handle); - image::Renderer::draw( - renderer, - handle, - Rectangle::new( - image_position, - Size::new( - image_size.width as f32 / scale_factor, - image_size.height as f32 / scale_factor, + if let Some(ref handle) = *handle_opt { + let image_size = image::Renderer::dimensions(renderer, &handle); + image::Renderer::draw( + renderer, + handle.clone(), + Rectangle::new( + image_position, + Size::new( + image_size.width as f32 / scale_factor, + image_size.height as f32 / scale_factor, + ), ), - ), - [0.0; 4], - ); + [0.0; 4], + ); + } // Draw scrollbar let scrollbar_alpha = match &state.dragging { @@ -661,7 +666,7 @@ pub struct State { scale_factor: Cell, scroll_pixels: f32, scrollbar_rect: Cell>, - handle: Mutex, + handle_opt: Mutex>, } impl State { @@ -673,8 +678,7 @@ impl State { scale_factor: Cell::new(1.0), scroll_pixels: 0.0, scrollbar_rect: Cell::new(Rectangle::default()), - //TODO: make option! - handle: Mutex::new(image::Handle::from_pixels(1, 1, vec![0, 0, 0, 0])), + handle_opt: Mutex::new(None), } } }