Skip to content

Commit 7d38506

Browse files
committed
Move state dimensions and bounds calculation out of the widget layout funcion
1 parent b7e9066 commit 7d38506

File tree

2 files changed

+116
-107
lines changed

2 files changed

+116
-107
lines changed

src/memory_editor.rs

Lines changed: 6 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -754,109 +754,8 @@ where
754754
.min_bounds()
755755
.width;
756756

757-
state.dimensions.byte_width = state.dimensions.char_width * 2.5;
758-
state.dimensions.group_spacing = state.dimensions.char_width;
759-
state.dimensions.section_separator_spacing = state.dimensions.char_width * 2.0;
760-
761-
state.dimensions.section_data_start = state.dimensions.char_width
762-
* state.dimensions.address_char_len as f32
763-
+ state.dimensions.section_separator_spacing;
764-
state.dimensions.section_ascii_start = state.dimensions.section_data_start
765-
+ state.dimensions.byte_width * options.row_length as f32
766-
+ (options.row_length as f32 / state.dimensions.group_char_len as f32 - 1.0)
767-
* state.dimensions.group_spacing
768-
+ state.dimensions.section_separator_spacing;
769-
770-
state.dimensions.address_separator_x =
771-
state.dimensions.section_data_start - state.dimensions.section_separator_spacing / 2.0;
772-
state.dimensions.ascii_separator_x =
773-
state.dimensions.section_ascii_start - state.dimensions.section_separator_spacing / 2.0;
774-
state.text.jumpto_len = state.text.jumpto_text.len() as f32 * state.dimensions.char_width;
775-
776-
let options_text = "Options";
777-
let options_width = options_text.len() as f32 * state.dimensions.char_width;
778-
state.bounds.options = Rectangle {
779-
x: limits.min().width,
780-
y: limits.max().height - state.dimensions.char_height * 1.5,
781-
width: options_width,
782-
height: state.dimensions.char_height * 1.5,
783-
};
784-
785-
let total_width = limits.max().width;
786-
let jumpto_x = (total_width - state.text.jumpto_len - options_width) / 2.0;
787-
let input_x = jumpto_x + state.text.jumpto_len + state.dimensions.char_width;
788-
789-
state.bounds.addr_input = Rectangle {
790-
x: input_x,
791-
y: limits.max().height - state.dimensions.char_height * 1.3,
792-
width: (state.dimensions.char_width + 1.0)
793-
* state.dimensions.address_char_len as f32
794-
* 1.1,
795-
height: state.dimensions.char_height * 1.1,
796-
};
797-
798-
let panel_bounds = Rectangle {
799-
x: state.dimensions.char_width * 0.5,
800-
y: limits.max().height
801-
- state.dimensions.char_height * 1.5
802-
- state.dimensions.char_height * 4.0,
803-
width: limits.max().width - state.dimensions.char_width,
804-
height: state.dimensions.char_height * 4.0,
805-
};
806-
807-
let label_width = 120.0;
808-
let offset_y = panel_bounds.y + state.dimensions.char_height * 0.5;
809-
let checkbox_size = state.dimensions.char_height * 0.8;
810-
let base_x = panel_bounds.x + state.dimensions.char_width + label_width;
811-
812-
state.bounds.show_ascii_checkbox = Rectangle {
813-
x: base_x + 3.0 * state.dimensions.char_width,
814-
y: offset_y + state.dimensions.char_height * 2.0,
815-
width: checkbox_size,
816-
height: checkbox_size,
817-
};
818-
819-
state.bounds.text_format = Rectangle {
820-
x: base_x + 2.0 * state.dimensions.char_width,
821-
y: offset_y + state.dimensions.char_height,
822-
width: state.dimensions.char_width * 3.0,
823-
height: state.dimensions.char_height,
824-
};
825-
826-
state.bounds.prev_format = Rectangle {
827-
x: base_x,
828-
y: offset_y + state.dimensions.char_height,
829-
width: state.dimensions.char_width,
830-
height: state.dimensions.char_height,
831-
};
832-
833-
state.bounds.next_format = Rectangle {
834-
x: base_x + 6.0 * state.dimensions.char_width,
835-
y: offset_y + state.dimensions.char_height,
836-
width: state.dimensions.char_width,
837-
height: state.dimensions.char_height,
838-
};
839-
840-
state.bounds.prev_row_length = Rectangle {
841-
x: base_x,
842-
y: offset_y,
843-
width: state.dimensions.char_width,
844-
height: state.dimensions.char_height,
845-
};
846-
847-
state.bounds.next_row_length = Rectangle {
848-
x: base_x + 6.0 * state.dimensions.char_width,
849-
y: offset_y,
850-
width: state.dimensions.char_width,
851-
height: state.dimensions.char_height,
852-
};
853-
854-
state.bounds.text_row_length = Rectangle {
855-
x: base_x + 2.0 * state.dimensions.char_width,
856-
y: offset_y,
857-
width: state.dimensions.char_width * 3.0,
858-
height: state.dimensions.char_height,
859-
};
757+
state.update_dimensions(options.row_length as f32);
758+
state.update_bounds(limits);
860759

861760
layout::Node::with_children(limits.max(), vec![])
862761
}
@@ -1052,11 +951,11 @@ where
1052951
delta: mouse::ScrollDelta::Lines { y, .. },
1053952
}) => {
1054953
let step = y.trunc() * options.row_length as f32;
1055-
if step.is_sign_negative() {
1056-
state.start_address = state.start_address.saturating_sub(step.abs() as usize);
954+
state.start_address = if step.is_sign_negative() {
955+
state.start_address.saturating_sub(step.abs() as usize)
1057956
} else {
1058-
state.start_address += step as usize;
1059-
}
957+
state.start_address + step as usize
958+
};
1060959
publish(self.data_update_message(state, options.row_length));
1061960
}
1062961
_ => (),

src/state.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,116 @@ impl Default for State {
109109
}
110110
}
111111

112+
impl State {
113+
pub(crate) fn update_dimensions(&mut self, row_length: f32) {
114+
self.dimensions.byte_width = self.dimensions.char_width * 2.5;
115+
self.dimensions.group_spacing = self.dimensions.char_width;
116+
self.dimensions.section_separator_spacing = self.dimensions.char_width * 2.0;
117+
118+
self.dimensions.section_data_start = self.dimensions.char_width
119+
* self.dimensions.address_char_len as f32
120+
+ self.dimensions.section_separator_spacing;
121+
self.dimensions.section_ascii_start = self.dimensions.section_data_start
122+
+ self.dimensions.byte_width * row_length
123+
+ (row_length / self.dimensions.group_char_len as f32 - 1.0)
124+
* self.dimensions.group_spacing
125+
+ self.dimensions.section_separator_spacing;
126+
127+
self.dimensions.address_separator_x =
128+
self.dimensions.section_data_start - self.dimensions.section_separator_spacing / 2.0;
129+
self.dimensions.ascii_separator_x =
130+
self.dimensions.section_ascii_start - self.dimensions.section_separator_spacing / 2.0;
131+
self.text.jumpto_len = self.text.jumpto_text.len() as f32 * self.dimensions.char_width;
132+
}
133+
134+
pub(crate) fn update_bounds(&mut self, limits: &iced::advanced::layout::Limits) {
135+
let options_text = "Options";
136+
let options_width = options_text.len() as f32 * self.dimensions.char_width;
137+
self.bounds.options = Rectangle {
138+
x: limits.min().width,
139+
y: limits.max().height - self.dimensions.char_height * 1.5,
140+
width: options_width,
141+
height: self.dimensions.char_height * 1.5,
142+
};
143+
144+
let total_width = limits.max().width;
145+
let jumpto_x = (total_width - self.text.jumpto_len - options_width) / 2.0;
146+
let input_x = jumpto_x + self.text.jumpto_len + self.dimensions.char_width;
147+
148+
self.bounds.addr_input = Rectangle {
149+
x: input_x,
150+
y: limits.max().height - self.dimensions.char_height * 1.3,
151+
width: (self.dimensions.char_width + 1.0)
152+
* self.dimensions.address_char_len as f32
153+
* 1.1,
154+
height: self.dimensions.char_height * 1.1,
155+
};
156+
157+
let panel_bounds = Rectangle {
158+
x: self.dimensions.char_width * 0.5,
159+
y: limits.max().height
160+
- self.dimensions.char_height * 1.5
161+
- self.dimensions.char_height * 4.0,
162+
width: limits.max().width - self.dimensions.char_width,
163+
height: self.dimensions.char_height * 4.0,
164+
};
165+
166+
let label_width = 120.0;
167+
let offset_y = panel_bounds.y + self.dimensions.char_height * 0.5;
168+
let checkbox_size = self.dimensions.char_height * 0.8;
169+
let base_x = panel_bounds.x + self.dimensions.char_width + label_width;
170+
171+
self.bounds.show_ascii_checkbox = Rectangle {
172+
x: base_x + 3.0 * self.dimensions.char_width,
173+
y: offset_y + self.dimensions.char_height * 2.0,
174+
width: checkbox_size,
175+
height: checkbox_size,
176+
};
177+
178+
self.bounds.text_format = Rectangle {
179+
x: base_x + 2.0 * self.dimensions.char_width,
180+
y: offset_y + self.dimensions.char_height,
181+
width: self.dimensions.char_width * 3.0,
182+
height: self.dimensions.char_height,
183+
};
184+
185+
self.bounds.prev_format = Rectangle {
186+
x: base_x,
187+
y: offset_y + self.dimensions.char_height,
188+
width: self.dimensions.char_width,
189+
height: self.dimensions.char_height,
190+
};
191+
192+
self.bounds.next_format = Rectangle {
193+
x: base_x + 6.0 * self.dimensions.char_width,
194+
y: offset_y + self.dimensions.char_height,
195+
width: self.dimensions.char_width,
196+
height: self.dimensions.char_height,
197+
};
198+
199+
self.bounds.prev_row_length = Rectangle {
200+
x: base_x,
201+
y: offset_y,
202+
width: self.dimensions.char_width,
203+
height: self.dimensions.char_height,
204+
};
205+
206+
self.bounds.next_row_length = Rectangle {
207+
x: base_x + 6.0 * self.dimensions.char_width,
208+
y: offset_y,
209+
width: self.dimensions.char_width,
210+
height: self.dimensions.char_height,
211+
};
212+
213+
self.bounds.text_row_length = Rectangle {
214+
x: base_x + 2.0 * self.dimensions.char_width,
215+
y: offset_y,
216+
width: self.dimensions.char_width * 3.0,
217+
height: self.dimensions.char_height,
218+
};
219+
}
220+
}
221+
112222
impl Focusable for State {
113223
fn is_focused(&self) -> bool {
114224
self.focused

0 commit comments

Comments
 (0)