Skip to main content

khora_editor/widgets/
paint.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Low-level painting helpers used by other widgets.
16//!
17//! Colour maths and the gradient live once in `khora_tool_ui::widgets`; the
18//! helpers here re-expose them under the editor's historical names, plus the
19//! icon / text conveniences that are just thin wrappers over the `UiBuilder`
20//! trait (no logic to share).
21
22use khora_sdk::editor_ui::{FontFamilyHint, Icon, TextAlign, UiBuilder};
23
24/// Returns `color` with its alpha channel replaced. See
25/// [`khora_tool_ui::widgets::with_alpha`].
26pub fn with_alpha(color: [f32; 4], alpha: f32) -> [f32; 4] {
27    khora_tool_ui::widgets::with_alpha(color, alpha)
28}
29
30/// Paints a vertical gradient. Adapter over
31/// [`khora_tool_ui::widgets::vertical_gradient`].
32pub fn paint_vertical_gradient(
33    ui: &mut dyn UiBuilder,
34    rect: [f32; 4],
35    top: [f32; 4],
36    bottom: [f32; 4],
37    steps: u32,
38) {
39    khora_tool_ui::widgets::vertical_gradient(ui, rect, top, bottom, steps);
40}
41
42/// 1-pixel horizontal hairline.
43pub fn paint_hairline_h(ui: &mut dyn UiBuilder, x: f32, y: f32, w: f32, color: [f32; 4]) {
44    ui.paint_line([x, y], [x + w, y], color, 1.0);
45}
46
47/// Paints a single Lucide icon glyph at the given position. The icon is
48/// rendered with the bundled `"icons"` font family — falls back to a single
49/// dot if that family isn't installed.
50pub fn paint_icon(ui: &mut dyn UiBuilder, pos: [f32; 2], icon: Icon, size: f32, color: [f32; 4]) {
51    ui.paint_text_styled(
52        pos,
53        icon.glyph(),
54        size,
55        color,
56        FontFamilyHint::Icons,
57        TextAlign::Left,
58    );
59}
60
61/// Paints proportional text at the given position with explicit size + color.
62pub fn paint_text_size(
63    ui: &mut dyn UiBuilder,
64    pos: [f32; 2],
65    text: &str,
66    size: f32,
67    color: [f32; 4],
68) {
69    ui.paint_text_styled(
70        pos,
71        text,
72        size,
73        color,
74        FontFamilyHint::Proportional,
75        TextAlign::Left,
76    );
77}