~zanneth/hirogen

2f0bdd6bfe1c3dbc3dd574a9375174d5aa243929 — Charles Magahern 2 years ago 91fe59c
Implement splash screen for empty window
3 files changed, 45 insertions(+), 18 deletions(-)

M src/app.rs
M src/document.rs
M src/document_window.rs
M src/app.rs => src/app.rs +3 -2
@@ 14,8 14,9 @@ use gtk::{
use std::cell::RefCell;
use std::rc::Rc;

pub const APPLICATION_ID:   &str = "com.zanneth.hexeditor";
pub const APPLICATION_NAME: &str = "hexeditor";
pub const APPLICATION_ID:       &str = "com.zanneth.hexeditor";
pub const APPLICATION_NAME:     &str = "hexeditor";
pub const APPLICATION_VERSION:  &str = env!("CARGO_PKG_VERSION");

struct State
{

M src/document.rs => src/document.rs +38 -13
@@ 8,8 8,9 @@ use std::path::PathBuf;
use gdk::prelude::*;
use gtk::StateFlags;
use gtk::prelude::*;
use pango::{AttrList, Attribute, FontDescription, Layout};
use pango::{Alignment, AttrList, Attribute, FontDescription, Layout};

use crate::app::{APPLICATION_NAME, APPLICATION_VERSION};
use crate::ascii::*;
use crate::gdk_additions::{Direction, RGBAExt};
use crate::guard_unwrap;


@@ 278,6 279,7 @@ impl Document
        let layout = self.text_layout.clone().unwrap();
        layout.set_text(&header_text);
        layout.set_attributes(None);
        layout.set_alignment(Alignment::Left);
        pangocairo::show_layout(&cr, &layout);
    }



@@ 748,25 750,45 @@ impl Document
        
        Some(clip_rect)
    }

    fn draw_hex_ascii_text(&mut self, cr: &cairo::Context, style: &gtk::StyleContext)
    
    fn draw_splash(&mut self, cr: &cairo::Context, style: &gtk::StyleContext)
    {
        // calculate which parts of the document are being drawn
        let layout = self.text_layout.clone().unwrap();
        let clip_rect = self.calculate_text_clip_rect(cr).unwrap();
        let char_height = self.glyph_pixel_height as f64;
        let imin = ((clip_rect.y as f64 - self.hex_text_origin_y).max(0.0) / char_height).floor() as usize;
        let imax = ((self.hex_text_origin_y + clip_rect.y as f64 + clip_rect.height as f64) / char_height).ceil() as usize;
        let origin = self.calculate_relative_origin();

        // read style context from the widget so that dark mode and light mode work properly
        let fg = style.get_color(StateFlags::NORMAL);
        cr.set_source_rgb(fg.red, fg.green, fg.blue);
        
        let selected_color = style.get_color(StateFlags::LINK).pango_rgb();
        let origin = self.calculate_relative_origin();
        let clip_rect = self.calculate_text_clip_rect(cr).unwrap();
        
        let layout = self.text_layout.clone().unwrap();
        layout.set_text(&format!("{} v{}\nby zanneth", APPLICATION_NAME, APPLICATION_VERSION));
        layout.set_alignment(Alignment::Center);
        
        let pixel_size = layout.get_pixel_size();
        cr.move_to(
            (origin.0 as i32 + clip_rect.width / 2 - pixel_size.0 / 2) as f64,
            (origin.1 as i32 + clip_rect.height / 2 - pixel_size.1 / 2) as f64
        );
        pangocairo::show_layout(&cr, &layout);
    }

    fn draw_hex_ascii_text(&mut self, cr: &cairo::Context, style: &gtk::StyleContext)
    {
        // draw hex and ascii views
        if let Some(data) = &self.data {
            // calculate which parts of the document are being drawn
            let layout = self.text_layout.clone().unwrap();
            let clip_rect = self.calculate_text_clip_rect(cr).unwrap();
            let char_height = self.glyph_pixel_height as f64;
            let imin = ((clip_rect.y as f64 - self.hex_text_origin_y).max(0.0) / char_height).floor() as usize;
            let imax = ((self.hex_text_origin_y + clip_rect.y as f64 + clip_rect.height as f64) / char_height).ceil() as usize;
            let origin = self.calculate_relative_origin();

            // read style context from the widget so that dark mode and light mode work properly
            let fg = style.get_color(StateFlags::NORMAL);
            cr.set_source_rgb(fg.red, fg.green, fg.blue);
            
            let selected_color = style.get_color(StateFlags::LINK).pango_rgb();
            
            // allocate space for both the data buffer as well as the displayable line string
            let data_len = data.len();
            let mut strbuf = String::new();


@@ 837,8 859,11 @@ impl Document
                // copy the fully rendered line into the pango layout and draw it.
                layout.set_text(&strbuf);
                layout.set_attributes(Some(&attrs));
                layout.set_alignment(Alignment::Center);
                pangocairo::show_layout(&cr, &layout);
            }
        } else {
            self.draw_splash(cr, style);
        }
    }
}

M src/document_window.rs => src/document_window.rs +4 -3
@@ 827,7 827,6 @@ impl DocumentWindow
            let mut document = self.document.borrow_mut();
            document.move_selection(&direction);
            self.hex_view_queue_redraw();
            self.reload_status_bar();

            // check to see if we must page up/down the scrolling window based on the new selection.
            if let Some(selection) = document.checked_selection() {


@@ 862,6 861,8 @@ impl DocumentWindow
        if let Some(page_scroll_dir) = page_scroll_dir {
            self.scroll_page(&page_scroll_dir);
        }
        
        self.reload_status_bar();
    }
    
    fn handle_hex_key_press(self: &DocumentWindowRef, hex: u8)


@@ 1276,11 1277,11 @@ impl DocumentWindow
        match document.checked_selection() {
            Some(selection) => {
                let range = &selection.byte_range;
                self.offset_label.set_text(&format!("Offset: 0x{:x}", range.start));
                self.offset_label.set_text(&format!("Offset: 0x{:x} ({0:})", range.start));
                
                let length = range.end - range.start;
                if length > 1 {
                    self.length_label.set_text(&format!("Length: 0x{:x}", range.end - range.start));
                    self.length_label.set_text(&format!("Length: 0x{:x} ({0:})", range.end - range.start));
                } else {
                    self.length_label.set_text("");
                }