working bar example

This commit is contained in:
Daniel Bulant 2024-10-12 16:29:33 +02:00
parent 8549937247
commit c720e64123
No known key found for this signature in database
11 changed files with 111 additions and 45 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxBlameSettings">
<option name="version" value="2" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/rshell.iml" filepath="$PROJECT_DIR$/.idea/rshell.iml" />
</modules>
</component>
</project>

11
.idea/rshell.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

44
Cargo.lock generated
View file

@ -203,9 +203,9 @@ dependencies = [
[[package]]
name = "ashpd"
version = "0.9.1"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfe7e0dd0ac5a401dc116ed9f9119cf9decc625600474cb41f0fc0a0050abc9a"
checksum = "4d43c03d9e36dd40cab48435be0b09646da362c278223ca535493877b2c1dee9"
dependencies = [
"async-fs",
"async-net",
@ -665,9 +665,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.1.28"
version = "1.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e80e3b6a3ab07840e1cae9b0666a63970dc28e8ed5ffbcdacbfc760c281bfc1"
checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945"
dependencies = [
"jobserver",
"libc",
@ -920,7 +920,7 @@ checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
[[package]]
name = "cushy"
version = "0.4.0"
source = "git+https://github.com/khonsulabs/cushy.git?branch=main#b0a870aceb5e61992dc02382a9334b7b32049e01"
source = "git+https://github.com/khonsulabs/cushy.git?branch=main#ab474e2b7dbe9a178f7038243adc213e1a292d99"
dependencies = [
"ahash",
"alot",
@ -950,7 +950,7 @@ dependencies = [
[[package]]
name = "cushy-macros"
version = "0.4.0"
source = "git+https://github.com/khonsulabs/cushy.git?branch=main#b0a870aceb5e61992dc02382a9334b7b32049e01"
source = "git+https://github.com/khonsulabs/cushy.git?branch=main#ab474e2b7dbe9a178f7038243adc213e1a292d99"
dependencies = [
"attribute-derive",
"manyhow",
@ -2083,9 +2083,9 @@ checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
[[package]]
name = "js-sys"
version = "0.3.70"
version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9"
dependencies = [
"wasm-bindgen",
]
@ -4703,9 +4703,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.93"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e"
dependencies = [
"cfg-if",
"once_cell",
@ -4714,9 +4714,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.93"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358"
dependencies = [
"bumpalo",
"log",
@ -4729,9 +4729,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.43"
version = "0.4.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed"
checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b"
dependencies = [
"cfg-if",
"js-sys",
@ -4741,9 +4741,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.93"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56"
dependencies = [
"quote 1.0.37",
"wasm-bindgen-macro-support",
@ -4751,9 +4751,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.93"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68"
dependencies = [
"proc-macro2",
"quote 1.0.37",
@ -4764,9 +4764,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.93"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"
checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d"
[[package]]
name = "wayland-backend"
@ -4879,9 +4879,9 @@ dependencies = [
[[package]]
name = "web-sys"
version = "0.3.70"
version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0"
checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112"
dependencies = [
"js-sys",
"wasm-bindgen",

View file

@ -1,2 +1,3 @@
[toolchain]
channel = "stable"
components = ["rust-src"]

19
src/bar/mod.rs Normal file
View file

@ -0,0 +1,19 @@
use cushy::{kludgine::app::winit::window::WindowLevel, widget::MakeWidget, Application, Open};
mod spotify;
pub fn start_bar(app: &mut impl Application) -> cushy::Result {
let mut window = spotify::spotify_controls()
.pad()
.into_window()
.transparent()
.app_name("rshell")
.decorated(false)
// .resizable(false)
.window_level(WindowLevel::AlwaysOnTop);
window.sans_serif_font_family.push(cushy::styles::FamilyOwned::Name("Iosevka NF".into()));
window
.open(app).map(|_| ())
}

View file

@ -2,12 +2,12 @@ use std::{sync::{Arc, Mutex}, thread, time::Duration};
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions};
use mpris::{LoopStatus, PlaybackStatus, PlayerFinder};
use cushy::{figures::{units::Lp, Size}, kludgine::{AnyTexture, LazyTexture}, styles::{components::WidgetBackground, Color, Dimension, DimensionRange}, value::{Destination, Dynamic, IntoReader, Source}, widget::MakeWidget, widgets::{image::ImageCornerRadius, Image}};
use cushy::{figures::{units::Lp, Size, Zero}, kludgine::{AnyTexture, LazyTexture}, styles::{components::{FontWeight, TextColor, WidgetBackground}, Color, CornerRadii, Dimension, DimensionRange, Weight}, value::{Destination, Dynamic, IntoReader, Source}, widget::MakeWidget, widgets::{image::ImageCornerRadius, Image}};
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
use image::{self, Rgb};
use image::{self, imageops::FilterType, Rgb};
use tokio::{runtime, task::JoinHandle};
use crate::vibrancy::Vibrancy;
use crate::{theme::{BG_DEFAULT, TEXT_SPOTIFY}, vibrancy::Vibrancy};
#[derive(PartialEq)]
struct PlayingTrack {
@ -28,13 +28,20 @@ pub fn spotify_controls() -> impl MakeWidget {
let (progress, track) = get_track_dynamics();
let (texture, vibrancy) = get_texture_dynamic(track.clone());
const IMAGE_SIZE: i32 = 16 /* lineheight */ + 2 * 6 /* padding */ + 8; // Why the 8 there? I don't know, but it works.
const IMAGE_SIDE: i32 = 16 /* lineheight */ + 2 * 6 /* padding */;
let image_size = Size::squared(DimensionRange::from(Dimension::Lp(Lp::points(IMAGE_SIDE))));
const CORNER_RADIUS: Dimension = Dimension::Lp(Lp::points(6));
Image::new(texture)
.aspect_fit()
// .with(&ImageCornerRadius, Lp::points(6))
.with(&ImageCornerRadius, CornerRadii {
top_left: CORNER_RADIUS,
top_right: Dimension::ZERO,
bottom_left: CORNER_RADIUS,
bottom_right: Dimension::ZERO,
})
// default pad is 6, default line height is 16
.size(Size::new(DimensionRange::from(Dimension::Lp(Lp::points(IMAGE_SIZE))), DimensionRange::from(Dimension::Lp(Lp::points(IMAGE_SIZE)))))
.size(image_size)
.and(
track.map_each(|track| {
if let Some(track) = track {
@ -48,12 +55,14 @@ pub fn spotify_controls() -> impl MakeWidget {
}
})
.to_label()
.with(&TextColor, TEXT_SPOTIFY)
.with(&FontWeight, Weight::BOLD)
.centered()
.pad()
)
.into_columns()
.with(&WidgetBackground, Color::CLEAR_WHITE) // vibrancy.map_each(|vib| vib.muted.unwrap_or(Color::BLACK).into()))
// .size(Size::new(DimensionRange::default(), DimensionRange::from(Dimension::Lp(Lp::points(28)))))
.with(&WidgetBackground, BG_DEFAULT)
// .with(&WidgetBackground, vibrancy.map_each(|vib| vib.primary.unwrap_or(Color::BLACK).into()))
}
fn get_empty_texture() -> AnyTexture {
@ -132,6 +141,7 @@ fn get_texture_dynamic(track: Dynamic<Option<PlayingTrack>>) -> (Dynamic<AnyText
let response = client.get(track_url).send().await.unwrap();
let bytes = response.bytes().await.unwrap();
let image = image::load_from_memory(&bytes).unwrap();
let image = image.resize(128, 128, FilterType::Lanczos3);
let image_vibrancy = Vibrancy::new(&image);
vibrancy.set(ImageVibrancy {
primary: image_vibrancy.primary.map(|c| rgb_to_color(c)),
@ -143,13 +153,11 @@ fn get_texture_dynamic(track: Dynamic<Option<PlayingTrack>>) -> (Dynamic<AnyText
});
let image_texture = LazyTexture::from_image(image, cushy::kludgine::wgpu::FilterMode::Linear);
let image_texture = AnyTexture::Lazy(image_texture);
texture.map_mut(move |mut t| *t = image_texture);
// texture.set(image_texture);
texture.set(image_texture);
}));
} else {
texture.map_mut(move |mut t| *t = get_empty_texture());
vibrancy.set(ImageVibrancy::default());
// texture.set(empty_texture);
texture.set(get_empty_texture());
}
}
}).persist();

View file

@ -1,19 +1,13 @@
use cushy::{styles::{components::DefaultBackgroundColor, Color}, widget::MakeWidget, Open, PendingApp, Run, TokioRuntime};
use bar::start_bar;
use cushy::{PendingApp, Run, TokioRuntime};
mod spotify;
mod vibrancy;
mod theme;
mod bar;
fn main() -> cushy::Result {
let mut app = PendingApp::new(TokioRuntime::default());
spotify::spotify_controls()
.centered()
.with(&DefaultBackgroundColor, Color::CLEAR_WHITE)
.into_window()
.transparent()
.app_name("rshell")
.open(&mut app)?;
start_bar(&mut app)?;
app.run()
}

5
src/theme.rs Normal file
View file

@ -0,0 +1,5 @@
use cushy::styles::Color;
pub const TEXT_SPOTIFY: Color = Color(0x1DB954FF);
pub const BG_DEFAULT: Color = Color(0x191724FF);