team-ljyzipelo

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