team-ljyzipelo

공개
660줄24 KB
1use eframe::egui::{self, Color32, FontId, Stroke, TextStyle};
2
3pub const BACKGROUND: Color32 = Color32::from_rgb(247, 248, 250);
4pub const PANEL: Color32 = Color32::WHITE;
5pub const STRIPE: Color32 = Color32::from_rgb(243, 245, 248);
6pub const BORDER: Color32 = Color32::from_rgb(217, 222, 229);
7pub const TEXT: Color32 = Color32::from_rgb(31, 41, 51);
8pub const MUTED: Color32 = Color32::from_rgb(91, 102, 115);
9pub const BLUE: Color32 = Color32::from_rgb(37, 99, 235);
10pub const BLUE_HOVER: Color32 = Color32::from_rgb(29, 78, 216);
11pub const BLUE_ACTIVE: Color32 = Color32::from_rgb(30, 64, 175);
12pub const SELECTED: Color32 = Color32::from_rgb(232, 240, 254);
13/// 파일 목록에서 선택한 행의 반투명 파란 배경 (BLUE 12%).
14pub const ROW_SELECTED: Color32 = Color32::from_rgba_unmultiplied_const(37, 99, 235, 31);
15/// 파일 목록에서 마우스를 올린 행의 배경. 선택한 행보다 조금 더 투명함 (BLUE 6%).
16pub const ROW_HOVER: Color32 = Color32::from_rgba_unmultiplied_const(37, 99, 235, 15);
17pub const DROP: Color32 = Color32::from_rgb(238, 244, 255);
18pub const DASH: Color32 = Color32::from_rgb(170, 180, 192);
19pub const SUCCESS: Color32 = Color32::from_rgb(21, 128, 61);
20pub const SUCCESS_BG: Color32 = Color32::from_rgb(232, 245, 236);
21pub const WARNING: Color32 = Color32::from_rgb(180, 83, 9);
22pub const WARNING_BG: Color32 = Color32::from_rgb(255, 247, 232);
23pub const ERROR: Color32 = Color32::from_rgb(185, 28, 28);
24pub const ERROR_BG: Color32 = Color32::from_rgb(253, 237, 237);
25pub const FOLDER: Color32 = Color32::from_rgb(240, 160, 0);
26pub const FILE: Color32 = Color32::from_rgb(123, 135, 148);
27pub const SPACE: f32 = 8.0;
28pub const PAD: i8 = 16;
29pub const DROP_PAD: i8 = 24;
30pub const DROP_HEIGHT: f32 = 280.0;
31pub const DROP_RADIUS: u8 = 12;
32pub const ROW: f32 = 32.0;
33pub const BUTTON: f32 = 40.0;
34/// 글자 크기 단계. 앱의 모든 글자는 이 세 값 중 하나를 씀.
35/// 기본 16px을 기준으로 보조 글자는 작게, 제목과 강조 글자는 크게 씀.
36pub const FONT_SMALL: f32 = 14.0;
37pub const FONT_BODY: f32 = 16.0;
38pub const FONT_LARGE: f32 = 20.0;
39/// 경로와 검색 입력란의 높이. 위쪽 경로 줄은 이 높이만큼만 차지함.
40pub const FIELD: f32 = 36.0;
41/// 테두리 없는 버튼에 마우스를 올리거나 누를 때의 배경.
42pub const HOVER: Color32 = Color32::from_rgb(229, 233, 239);
43pub const PRESS: Color32 = Color32::from_rgb(214, 220, 229);
44
45/// 앱에 포함한 기본 글꼴 (SIL OFL 1.1, assets/fonts/OFL.txt).
46pub const PRETENDARD: &[u8] = include_bytes!("assets/fonts/Pretendard-Regular.ttf");
47
48/// Pretendard를 기본 글꼴로 쓰고, 없는 글자는 운영체제 한글 글꼴로 대신 그림.
49pub fn fonts(fallback: Option<(Vec<u8>, u32)>) -> egui::FontDefinitions {
50 let mut fonts = egui::FontDefinitions::empty();
51 fonts.font_data.insert(
52 "pretendard".into(),
53 egui::FontData::from_static(PRETENDARD).into(),
54 );
55 let mut family = vec!["pretendard".to_owned()];
56 if let Some((bytes, index)) = fallback {
57 let mut data = egui::FontData::from_owned(bytes);
58 data.index = index;
59 fonts.font_data.insert("system-kr".into(), data.into());
60 family.push("system-kr".into());
61 }
62 fonts
63 .families
64 .insert(egui::FontFamily::Proportional, family.clone());
65 fonts.families.insert(egui::FontFamily::Monospace, family);
66 fonts
67}
68
69pub fn apply(ctx: &egui::Context) {
70 ctx.set_theme(egui::Theme::Light);
71 ctx.set_visuals(egui::Visuals::light());
72 ctx.style_mut_of(egui::Theme::Light, |style| {
73 style
74 .text_styles
75 .insert(TextStyle::Body, FontId::proportional(FONT_BODY));
76 style
77 .text_styles
78 .insert(TextStyle::Button, FontId::proportional(FONT_BODY));
79 style
80 .text_styles
81 .insert(TextStyle::Small, FontId::proportional(FONT_SMALL));
82 style
83 .text_styles
84 .insert(TextStyle::Heading, FontId::proportional(FONT_LARGE));
85 style.visuals.override_text_color = Some(TEXT);
86 style.visuals.panel_fill = BACKGROUND;
87 style.visuals.window_fill = PANEL;
88 style.visuals.extreme_bg_color = PANEL;
89 style.visuals.faint_bg_color = STRIPE;
90 style.visuals.selection.bg_fill = SELECTED;
91 style.visuals.selection.stroke = Stroke::new(2.0, BLUE);
92 style.visuals.widgets.inactive.bg_fill = PANEL;
93 style.visuals.widgets.inactive.weak_bg_fill = PANEL;
94 style.visuals.widgets.inactive.bg_stroke = Stroke::new(1.0, BORDER);
95 style.visuals.widgets.hovered.bg_fill = DROP;
96 style.visuals.widgets.hovered.weak_bg_fill = DROP;
97 style.visuals.widgets.hovered.bg_stroke = Stroke::new(1.0, BLUE);
98 style.visuals.widgets.active.bg_stroke = Stroke::new(2.0, BLUE);
99 for widget in [
100 &mut style.visuals.widgets.inactive,
101 &mut style.visuals.widgets.hovered,
102 &mut style.visuals.widgets.active,
103 &mut style.visuals.widgets.noninteractive,
104 ] {
105 widget.corner_radius = egui::CornerRadius::same(6);
106 widget.fg_stroke = Stroke::new(1.5, TEXT);
107 }
108 style.visuals.window_corner_radius = egui::CornerRadius::same(8);
109 style.spacing.item_spacing = egui::vec2(SPACE, SPACE);
110 style.spacing.button_padding = egui::vec2(16.0, SPACE);
111 style.spacing.interact_size.y = ROW;
112 style.spacing.window_margin = egui::Margin::same(PAD);
113 style.visuals.disabled_alpha = 0.4;
114 // 클릭과 표시가 즉시 바뀌도록 서서히 나타남, 부드러운 스크롤, 커서 깜빡임을 모두 끔.
115 style.animation_time = 0.0;
116 style.scroll_animation = egui::style::ScrollAnimation::none();
117 style.visuals.text_cursor.blink = false;
118 });
119}
120
121pub fn focus(response: &egui::Response) {
122 if response.has_focus() {
123 response.ctx.layer_painter(response.layer_id).rect_stroke(
124 response.rect.expand(2.0),
125 6,
126 Stroke::new(2.0, BLUE),
127 egui::StrokeKind::Outside,
128 );
129 }
130}
131
132pub fn action(ui: &mut egui::Ui, text: &str, primary: bool, enabled: bool) -> egui::Response {
133 let size = egui::vec2(
134 if primary { 112.0 } else { 0.0 },
135 if primary { BUTTON } else { 36.0 },
136 );
137 action_sized(ui, text, primary, enabled, size)
138}
139
140/// 정한 최소 크기로 버튼을 그림. 함께 놓이는 버튼끼리 크기를 맞출 때 씀.
141pub fn action_sized(
142 ui: &mut egui::Ui,
143 text: &str,
144 primary: bool,
145 enabled: bool,
146 size: egui::Vec2,
147) -> egui::Response {
148 let response = ui
149 .scope(|ui| {
150 if primary {
151 let style = ui.style_mut();
152 style.visuals.widgets.inactive.bg_fill = BLUE;
153 style.visuals.widgets.inactive.weak_bg_fill = BLUE;
154 style.visuals.widgets.hovered.bg_fill = BLUE_HOVER;
155 style.visuals.widgets.hovered.weak_bg_fill = BLUE_HOVER;
156 style.visuals.widgets.active.bg_fill = BLUE_ACTIVE;
157 style.visuals.widgets.active.weak_bg_fill = BLUE_ACTIVE;
158 }
159 ui.add_enabled(
160 enabled,
161 egui::Button::new(egui::RichText::new(text).strong().color(if primary {
162 PANEL
163 } else if text == "취소" {
164 ERROR
165 } else {
166 TEXT
167 }))
168 .min_size(size)
169 .stroke(Stroke::new(1.0, if primary { BLUE } else { BORDER })),
170 )
171 })
172 .inner;
173 focus(&response);
174 response
175}
176
177pub const STATUS_HEIGHT: f32 = 34.0;
178
179/// 파일 목록 행 배경. 선택한 행은 마우스를 올려도 선택 색을 유지하고,
180/// 선택하지 않은 행에 마우스를 올리면 더 옅은 파란색을 칠함.
181pub fn row_background(ui: &egui::Ui, rect: egui::Rect, selected: bool, hovered: bool) {
182 let fill = if selected {
183 ROW_SELECTED
184 } else if hovered {
185 ROW_HOVER
186 } else {
187 return;
188 };
189 ui.painter().rect_filled(rect, 0, fill);
190}
191
192/// 경로 줄 높이를 꽉 채우는 입력란을 그림. 모서리는 각지게 하고,
193/// 위 테두리는 상단 메뉴의 구분선이, 아래 테두리는 경로 줄의 아래 선이 대신해
194/// 선이 겹쳐 보이지 않게 함. 초점이 있으면 파란 테두리.
195pub fn field_frame(ui: &egui::Ui, rect: egui::Rect, focused: bool) {
196 let painter = ui.painter();
197 painter.rect_filled(rect, 0, PANEL);
198 if focused {
199 painter.rect_stroke(rect, 0, Stroke::new(1.0, BLUE), egui::StrokeKind::Inside);
200 return;
201 }
202 let stroke = Stroke::new(1.0, BORDER);
203 painter.vline(rect.left() + 0.5, rect.y_range(), stroke);
204 painter.vline(rect.right() - 0.5, rect.y_range(), stroke);
205}
206
207/// 네 변 테두리가 모두 있는 입력란 모양 (주변에 구분선이 없는 곳에서 씀).
208pub fn boxed_field(ui: &egui::Ui, rect: egui::Rect) {
209 ui.painter().rect(
210 rect,
211 0,
212 PANEL,
213 Stroke::new(1.0, BORDER),
214 egui::StrokeKind::Inside,
215 );
216}
217
218/// 입력란 안에 경로처럼 한 줄 글자를 그림. 넘치면 줄이고 전체 글자는 툴팁으로 보여 줌.
219/// 부모 배치에 영향을 주지 않는 하위 영역을 써서 옆 칸의 위치가 흔들리지 않게 함.
220pub fn field_text(ui: &mut egui::Ui, rect: egui::Rect, text: &str) {
221 let mut child = ui.new_child(
222 egui::UiBuilder::new()
223 .max_rect(rect.shrink2(egui::vec2(10.0, 0.0)))
224 .layout(egui::Layout::left_to_right(egui::Align::Center)),
225 );
226 bounded_text(&mut child, text, (rect.width() - 20.0).max(0.0));
227}
228
229/// 상태 줄 글자를 칸의 세로 가운데에 그림. 넘치면 줄이고 전체 글자는 툴팁으로 보여 줌.
230pub fn status_text(
231 ui: &mut egui::Ui,
232 rect: egui::Rect,
233 text: &str,
234 width: f32,
235 align: egui::Align,
236) {
237 let mut job = egui::text::LayoutJob::simple_singleline(
238 text.into(),
239 FontId::proportional(FONT_BODY),
240 TEXT,
241 );
242 job.wrap = egui::text::TextWrapping {
243 max_width: width.max(0.0),
244 max_rows: 1,
245 ..Default::default()
246 };
247 let galley = ui.fonts_mut(|fonts| fonts.layout_job(job));
248 let size = galley.size();
249 let x = if align == egui::Align::Max {
250 rect.right() - size.x
251 } else {
252 rect.left()
253 };
254 let text_rect = egui::Rect::from_min_size(egui::pos2(x, rect.center().y - size.y / 2.0), size);
255 ui.painter().galley(text_rect.min, galley, TEXT);
256 let response = ui.interact(
257 text_rect,
258 ui.scope_id()
259 .with(("status-text", align == egui::Align::Max)),
260 egui::Sense::hover(),
261 );
262 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Label, true, text));
263 response.on_hover_text(text);
264}
265
266pub fn panel(fill: Color32) -> egui::Frame {
267 egui::Frame::new()
268 .fill(fill)
269 .inner_margin(egui::Margin::same(PAD))
270 .corner_radius(8)
271}
272
273#[derive(Clone, Copy)]
274pub enum Icon {
275 File,
276 Folder,
277 Archive,
278 Lock,
279 Success,
280 Warning,
281 Error,
282}
283
284pub fn icon(ui: &mut egui::Ui, kind: Icon, size: f32) {
285 let (rect, _) = ui.allocate_exact_size(egui::vec2(size, size), egui::Sense::hover());
286 let p = ui.painter();
287 let color = match kind {
288 Icon::Folder => FOLDER,
289 Icon::Archive => BLUE,
290 Icon::Lock | Icon::Warning => WARNING,
291 Icon::Success => SUCCESS,
292 Icon::Error => ERROR,
293 Icon::File => FILE,
294 };
295 let s = Stroke::new(if size > 32.0 { 2.5 } else { 1.5 }, color);
296 let point = |x, y| rect.min + egui::vec2(x * size, y * size);
297 let box_at = |x, y, w, h| egui::Rect::from_min_max(point(x, y), point(x + w, y + h));
298 match kind {
299 Icon::Folder => {
300 p.rect_filled(box_at(0.08, 0.22, 0.42, 0.22), 2, color);
301 p.rect_filled(box_at(0.08, 0.34, 0.84, 0.50), 2, color);
302 }
303 Icon::File | Icon::Archive => {
304 p.rect_stroke(
305 box_at(0.19, 0.08, 0.62, 0.84),
306 2,
307 s,
308 egui::StrokeKind::Inside,
309 );
310 p.line_segment([point(0.60, 0.08), point(0.60, 0.30)], s);
311 p.line_segment([point(0.60, 0.30), point(0.81, 0.30)], s);
312 if matches!(kind, Icon::Archive) {
313 for y in [0.42, 0.57, 0.72] {
314 p.line_segment([point(0.39, y), point(0.59, y)], s);
315 }
316 }
317 }
318 Icon::Lock => {
319 p.circle_stroke(point(0.50, 0.35), size * 0.23, s);
320 p.rect_filled(box_at(0.18, 0.39, 0.64, 0.47), 2, color);
321 }
322 Icon::Success | Icon::Error => {
323 p.circle_stroke(rect.center(), size * 0.40, s);
324 if matches!(kind, Icon::Success) {
325 p.line_segment([point(0.28, 0.50), point(0.44, 0.66)], s);
326 p.line_segment([point(0.44, 0.66), point(0.74, 0.33)], s);
327 } else {
328 p.line_segment([point(0.34, 0.34), point(0.66, 0.66)], s);
329 p.line_segment([point(0.66, 0.34), point(0.34, 0.66)], s);
330 }
331 }
332 Icon::Warning => {
333 p.add(egui::Shape::closed_line(
334 vec![point(0.50, 0.08), point(0.94, 0.86), point(0.06, 0.86)],
335 s,
336 ));
337 p.line_segment([point(0.50, 0.32), point(0.50, 0.57)], s);
338 p.circle_filled(point(0.50, 0.73), size * 0.035, color);
339 }
340 }
341}
342
343pub fn bounded_text(ui: &mut egui::Ui, text: &str, width: f32) -> egui::Response {
344 bounded_text_hover(ui, text, text, width)
345}
346
347/// 폭에 맞춰 줄인 글자를 그리고, 마우스를 올리면 hover 글자를 보여 줌.
348pub fn bounded_text_hover(
349 ui: &mut egui::Ui,
350 text: &str,
351 hover: &str,
352 width: f32,
353) -> egui::Response {
354 let width = width.max(0.0);
355 ui.allocate_ui_with_layout(
356 egui::vec2(width, ui.text_style_height(&TextStyle::Body)),
357 ui.layout().clone(),
358 |ui| {
359 ui.set_min_width(width);
360 ui.set_max_width(width);
361 ui.add(egui::Label::new(text).truncate())
362 },
363 )
364 .inner
365 .on_hover_text(hover)
366}
367
368pub fn window_title(ui: &egui::Ui, text: &str, width: f32) -> String {
369 let mut job = egui::text::LayoutJob::simple_singleline(
370 text.into(),
371 FontId::proportional(FONT_BODY),
372 TEXT,
373 );
374 job.wrap = egui::text::TextWrapping {
375 max_width: width.max(0.0),
376 max_rows: 1,
377 break_anywhere: true,
378 ..Default::default()
379 };
380 ui.fonts_mut(|fonts| fonts.layout_job(job))
381 .rows
382 .iter()
383 .flat_map(|row| row.glyphs.iter().map(|glyph| glyph.chr))
384 .collect()
385}
386
387pub fn column_label(
388 ui: &mut egui::Ui,
389 text: impl Into<egui::WidgetText>,
390 width: f32,
391 align: egui::Align,
392) -> egui::Response {
393 let layout = if align == egui::Align::Max {
394 egui::Layout::right_to_left(egui::Align::Center)
395 } else {
396 egui::Layout::left_to_right(egui::Align::Center)
397 };
398 ui.allocate_ui_with_layout(egui::vec2(width, ROW), layout, |ui| {
399 ui.set_min_width(width);
400 ui.set_max_width(width);
401 ui.add(egui::Label::new(text).truncate())
402 })
403 .inner
404}
405
406pub fn action_pair(
407 ui: &mut egui::Ui,
408 secondary: &str,
409 primary: Option<(&str, bool)>,
410) -> (bool, bool) {
411 let text_width = |text: &str| {
412 ui.painter()
413 .layout_no_wrap(text.into(), FontId::proportional(FONT_BODY), TEXT)
414 .size()
415 .x
416 };
417 // 두 버튼은 긴 쪽 글자에 맞춘 같은 크기로 그림.
418 let button = egui::vec2(
419 primary
420 .map_or(0.0, |(label, _)| text_width(label))
421 .max(text_width(secondary))
422 + 32.0,
423 BUTTON,
424 )
425 .max(egui::vec2(112.0, BUTTON));
426 let count = if primary.is_some() { 2.0 } else { 1.0 };
427 let width = button.x * count + SPACE * (count - 1.0);
428 ui.horizontal(|ui| {
429 ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
430 ui.add_space((ui.available_width() - width).max(0.0));
431 let secondary = action_sized(ui, secondary, false, true, button).clicked();
432 let primary = primary.is_some_and(|(label, enabled)| {
433 action_sized(ui, label, true, enabled, button)
434 .on_disabled_hover_text(match label {
435 "만들기" => "묶을 파일이나 폴더를 추가하세요",
436 "다른 폴더에 풀기" => "기존 풀 위치 폴더를 먼저 확인하세요",
437 _ => "입력한 내용을 확인하세요",
438 })
439 .clicked()
440 });
441 (secondary, primary)
442 })
443 .inner
444 })
445 .inner
446}
447
448pub fn indeterminate(ui: &mut egui::Ui) {
449 let (rect, response) = ui.allocate_exact_size(
450 egui::vec2(ui.available_width(), SPACE * 2.0),
451 egui::Sense::hover(),
452 );
453 // 움직이는 표시 없이 전체 막대를 옅은 파란색으로 채워 진행 중임을 나타냄.
454 ui.painter()
455 .rect_filled(rect, 8, Color32::from_rgb(191, 211, 248));
456 response.widget_info(|| {
457 egui::WidgetInfo::labeled(
458 egui::Role::ProgressIndicator,
459 ui.is_enabled(),
460 "전체 크기를 알 수 없는 작업 진행 중",
461 )
462 });
463}
464
465pub const OPEN: &str = include_str!("assets/icons/open-archive.svg");
466pub const CREATE: &str = include_str!("assets/icons/create-zip.svg");
467pub const EXTRACT: &str = include_str!("assets/icons/extract-archive.svg");
468pub const UP: &str = include_str!("assets/icons/navigate-up.svg");
469pub const CLOSE: &str = include_str!("assets/icons/close-dialog.svg");
470
471pub fn svg_icon(painter: &egui::Painter, rect: egui::Rect, asset: &str, color: Color32) {
472 for element in asset.split("points=\"").skip(1) {
473 let points = element
474 .split('"')
475 .next()
476 .unwrap_or_default()
477 .split_whitespace()
478 .filter_map(|pair| {
479 let (x, y) = pair.split_once(',')?;
480 Some(
481 rect.min
482 + egui::vec2(x.parse::<f32>().ok()?, y.parse::<f32>().ok()?)
483 * (rect.width() / 24.0),
484 )
485 })
486 .collect::<Vec<_>>();
487 let width = rect.width() / 18.0;
488 // 꼭짓점과 끝점에 원을 더해 둥근 이음과 둥근 끝을 만듦.
489 for point in &points {
490 painter.circle_filled(*point, width / 2.0, color);
491 }
492 painter.add(egui::Shape::line(points, Stroke::new(width, color)));
493 }
494}
495
496/// 가장 긴 이름(압축해제, FONT_SMALL에서 약 48px) 양옆에 12px씩 남는 폭.
497pub const TOOL_SIZE: egui::Vec2 = egui::vec2(72.0, 60.0);
498const TOOL_ICON: f32 = 30.0;
499const TOOL_LABEL: f32 = FONT_SMALL;
500/// 아이콘 그림 아래와 이름 글자 위 사이의 보이는 간격.
501const TOOL_GAP: f32 = 6.0;
502
503/// SVG 폴리라인 점들의 세로 범위(24 단위 좌표).
504fn svg_y_range(asset: &str) -> Option<(f32, f32)> {
505 asset
506 .split("points=\"")
507 .skip(1)
508 .flat_map(|element| {
509 element
510 .split('"')
511 .next()
512 .unwrap_or_default()
513 .split_whitespace()
514 .filter_map(|pair| pair.split_once(',')?.1.parse::<f32>().ok())
515 .collect::<Vec<_>>()
516 })
517 .fold(None, |range: Option<(f32, f32)>, y| {
518 Some(range.map_or((y, y), |(lo, hi)| (lo.min(y), hi.max(y))))
519 })
520}
521
522/// 상단 메뉴 아이콘 세 개를 합친 그림 범위. 모든 버튼이 같은 기준을 써서 이름 높이가 맞음.
523fn tool_icon_ink() -> (f32, f32) {
524 let scale = TOOL_ICON / 24.0;
525 let half_stroke = TOOL_ICON / 18.0 / 2.0;
526 let (lo, hi) = [OPEN, CREATE, EXTRACT]
527 .iter()
528 .filter_map(|asset| svg_y_range(asset))
529 .fold((24.0f32, 0.0f32), |(lo, hi), (a, b)| (lo.min(a), hi.max(b)));
530 (lo * scale - half_stroke, hi * scale + half_stroke)
531}
532
533/// 글자 줄 상자가 아니라 실제로 그려지는 글자 모양의 위아래 범위.
534fn galley_ink(galley: &egui::Galley) -> (f32, f32) {
535 galley
536 .rows
537 .iter()
538 .flat_map(|row| {
539 row.glyphs
540 .iter()
541 .filter(|g| g.uv_rect.size.y > 0.0)
542 .map(move |g| {
543 let top = row.pos.y + g.pos.y + g.uv_rect.offset.y;
544 (top, top + g.uv_rect.size.y)
545 })
546 })
547 .fold(None, |range: Option<(f32, f32)>, (top, bottom)| {
548 Some(range.map_or((top, bottom), |(a, b)| (a.min(top), b.max(bottom))))
549 })
550 .unwrap_or((0.0, galley.size().y))
551}
552
553pub fn tool(
554 ui: &mut egui::Ui,
555 label: &str,
556 asset: &str,
557 enabled: bool,
558 blue: bool,
559) -> egui::Response {
560 let (rect, response) = ui.allocate_exact_size(
561 TOOL_SIZE,
562 if enabled {
563 egui::Sense::click()
564 } else {
565 egui::Sense::hover()
566 },
567 );
568 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, label));
569 let color = if enabled {
570 if blue { BLUE } else { TEXT }
571 } else {
572 MUTED
573 };
574 if enabled && (response.hovered() || response.has_focus()) {
575 let fill = if response.is_pointer_button_down_on() {
576 PRESS
577 } else {
578 HOVER
579 };
580 ui.painter().rect_filled(rect, 8, fill);
581 }
582 // 아이콘 그림 위쪽부터 글자 모양 아래쪽까지를 하나로 보고 버튼 가운데에 둠.
583 // 그래서 마우스를 올려 배경이 보일 때 위아래 여백이 같음.
584 let galley =
585 ui.painter()
586 .layout_no_wrap(label.to_owned(), FontId::proportional(TOOL_LABEL), color);
587 let (icon_top, icon_bottom) = tool_icon_ink();
588 let (text_top, text_bottom) = galley_ink(&galley);
589 let block = (icon_bottom - icon_top) + TOOL_GAP + (text_bottom - text_top);
590 let start = rect.top() + (rect.height() - block) / 2.0;
591 let icon_rect = egui::Rect::from_min_size(
592 egui::pos2(rect.center().x - TOOL_ICON / 2.0, start - icon_top),
593 egui::vec2(TOOL_ICON, TOOL_ICON),
594 );
595 svg_icon(ui.painter(), icon_rect, asset, color);
596 let text_y = start + (icon_bottom - icon_top) + TOOL_GAP - text_top;
597 ui.painter().galley(
598 egui::pos2(rect.center().x - galley.size().x / 2.0, text_y).round(),
599 galley,
600 color,
601 );
602 focus(&response);
603 response
604}
605
606/// 테두리와 배경 없이 아이콘만 그리는 버튼. 사용할 수 있을 때만 마우스를 올리면 배경이 어두워짐.
607pub fn flat_icon_button(
608 ui: &mut egui::Ui,
609 name: &str,
610 asset: &str,
611 enabled: bool,
612 size: egui::Vec2,
613) -> egui::Response {
614 let (rect, response) = ui.allocate_exact_size(
615 size,
616 if enabled {
617 egui::Sense::click()
618 } else {
619 egui::Sense::hover()
620 },
621 );
622 let response = response.on_hover_text(name);
623 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, name));
624 if enabled && response.hovered() {
625 let fill = if response.is_pointer_button_down_on() {
626 PRESS
627 } else {
628 HOVER
629 };
630 ui.painter().rect_filled(rect, 6, fill);
631 }
632 svg_icon(
633 ui.painter(),
634 egui::Rect::from_center_size(rect.center(), egui::vec2(20.0, 20.0)),
635 asset,
636 if enabled { TEXT } else { MUTED },
637 );
638 focus(&response);
639 response
640}
641
642pub const SEARCH: &str = include_str!("assets/icons/search-files.svg");
643
644pub fn icon_button(ui: &mut egui::Ui, name: &str, asset: &str, enabled: bool) -> egui::Response {
645 let response = ui
646 .add_enabled(
647 enabled,
648 egui::Button::new("").min_size(egui::vec2(38.0, 36.0)),
649 )
650 .on_hover_text(name);
651 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, name));
652 svg_icon(
653 ui.painter(),
654 egui::Rect::from_center_size(response.rect.center(), egui::vec2(22.0, 22.0)),
655 asset,
656 if enabled { TEXT } else { MUTED },
657 );
658 focus(&response);
659 response
660}