resize_layer_dialog.rs - icy_draw - icy_draw is the successor to mystic draw. fork / mirror
 (HTM) git clone https://git.drkhsh.at/icy_draw.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       resize_layer_dialog.rs (2962B)
       ---
            1 use eframe::egui::{self, Layout};
            2 use egui_modal::Modal;
            3 use i18n_embed_fl::fl;
            4 use icy_engine::TextPane;
            5 
            6 use crate::{AnsiEditor, Message, ModalDialog, TerminalResult};
            7 
            8 pub struct ResizeLayerDialog {
            9     should_commit: bool,
           10     layer: usize,
           11 
           12     width: i32,
           13     height: i32,
           14 }
           15 
           16 impl ResizeLayerDialog {
           17     pub fn new(buf: &icy_engine::Buffer, layer: usize) -> Self {
           18         ResizeLayerDialog {
           19             should_commit: false,
           20             width: buf.layers[layer].get_width(),
           21             height: buf.layers[layer].get_height(),
           22             layer,
           23         }
           24     }
           25 }
           26 
           27 impl ModalDialog for ResizeLayerDialog {
           28     fn show(&mut self, ctx: &egui::Context) -> bool {
           29         let mut result = false;
           30         let modal = Modal::new(ctx, "set_canvas_size_dialog");
           31 
           32         modal.show(|ui| {
           33             ui.set_width(250.);
           34 
           35             modal.title(ui, fl!(crate::LANGUAGE_LOADER, "edit-canvas-size-title"));
           36 
           37             modal.frame(ui, |ui| {
           38                 egui::Grid::new("some_unique_id").num_columns(2).spacing([4.0, 8.0]).show(ui, |ui| {
           39                     ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
           40                         ui.label(fl!(crate::LANGUAGE_LOADER, "edit-canvas-size-width-label"));
           41                     });
           42                     let mut tmp_str = self.width.to_string();
           43                     ui.add(egui::TextEdit::singleline(&mut tmp_str).char_limit(35));
           44                     if let Ok(new_width) = tmp_str.parse::<i32>() {
           45                         self.width = new_width;
           46                     }
           47                     ui.end_row();
           48 
           49                     ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
           50                         ui.label(fl!(crate::LANGUAGE_LOADER, "edit-canvas-size-height-label"));
           51                     });
           52                     let mut tmp_str = self.height.to_string();
           53                     ui.add(egui::TextEdit::singleline(&mut tmp_str).char_limit(35));
           54                     if let Ok(new_height) = tmp_str.parse::<i32>() {
           55                         self.height = new_height;
           56                     }
           57                     ui.end_row();
           58                 });
           59                 ui.add_space(4.0);
           60             });
           61 
           62             modal.buttons(ui, |ui| {
           63                 if ui.button(fl!(crate::LANGUAGE_LOADER, "edit-canvas-size-resize")).clicked() {
           64                     self.should_commit = true;
           65                     result = true;
           66                 }
           67                 if ui.button(fl!(crate::LANGUAGE_LOADER, "new-file-cancel")).clicked() {
           68                     result = true;
           69                 }
           70             });
           71         });
           72         modal.open();
           73         result
           74     }
           75 
           76     fn should_commit(&self) -> bool {
           77         self.should_commit
           78     }
           79 
           80     fn commit(&self, editor: &mut AnsiEditor) -> TerminalResult<Option<Message>> {
           81         editor
           82             .buffer_view
           83             .lock()
           84             .get_edit_state_mut()
           85             .set_layer_size(self.layer, (self.width, self.height))?;
           86         Ok(None)
           87     }
           88 }