Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic spin loader #389

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/shared/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,43 @@ live_design! {
}
}
}

// A generic spinner widget styled for Robrix.
pub RobrixSpinLoader = <View> {
width: Fill, height: Fill
show_bg: true,
draw_bg: {
color: #00000000

instance rotation_speed: 1.
instance num_segments: 5.
fn pixel(self) -> vec4 {
// Normalize and adjust for aspect ratio to ensure circular shape.
let aspect = self.rect_size.x / self.rect_size.y;
let normalized_pos_x = (self.pos.x - 0.5) * max(aspect, 1.);
let normalized_pos_y = (self.pos.y - 0.5) * max(1. / aspect, 1.);
let pos = vec2(normalized_pos_x, normalized_pos_y) * 2.8;

let radius = length(pos);

// Rotate over time
let angle_offset = self.time * self.rotation_speed * PI * 2.0;
let angle = atan(pos.y, pos.x) + angle_offset;

// Create a circular shape with thickness
let inner_radius = 0.5;
let outer_radius = 0.7;
let thickness = outer_radius - inner_radius;

let edge = abs(radius - (inner_radius + thickness * 0.5)) - thickness * 0.5;
let d = smoothstep(0.01, -0.01, edge);

// 5 segments for the spinner.
// Use sin to create an effect where parts of the circle fade in and out.
let alpha = (sin(angle * self.num_segments) * 0.5 + 0.5) * d;

return vec4(self.color.rgb, alpha);
}
}
}
}