Skip to content

Commit

Permalink
Merge pull request #19 from dastansam/refactor-crate-build-script
Browse files Browse the repository at this point in the history
Refactor build script and expose build functions for crate dependents
  • Loading branch information
AaronErhardt authored Oct 25, 2024
2 parents 98ffe77 + 4120f43 commit c86eda6
Show file tree
Hide file tree
Showing 3,929 changed files with 248 additions and 218 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ keywords = ["gui", "gtk", "gtk4", "elm"]
categories = ["gui"]

[workspace]
members = ["update_icons"]
members = ["update_icons", "build_icons"]

[dependencies]
gtk = { version = "0.9", package = "gtk4" }

[build-dependencies]
gvdb = { version = "0.6.1", features = ["gresource"] }
serde = { version = "1.0.204", features = ["derive"] }
toml = "0.8.14"

[package.metadata.docs.rs]
all-features = true
# enable unstable features in the documentation
Expand Down
47 changes: 30 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,49 @@ For browsing all shipped icons:

> Sometimes, icons-development-kit and Fluent UI System Icons have overlapping icon names, so the postfix "-alt" is added.
### 2. Specify your icons 🖼️

Create a file called `icons.toml` next to the `Cargo.toml` file of your app:
### 2. Add Relm4 icons ✍

```toml
# Recommended: Specify your app ID *OR* your base resource path for more robust icon loading
app_id = "com.my.app"
base_resource_path = "/com/my/app/"

# List of icon names you found (shipped with this crate)
# Note: the file ending `-symbolic.svg` isn't part of the icon name.
icons = ["plus", "minus"]
relm4-icons = "0.8.0"

# Optional: Specify a folder containing your own SVG icons
icon_folder = "my_svg_icons"
[build-dependencies]
relm4-icons-build = "0.8.0"
```

### 3. Add the icons to your project 📦

### 3. Add Relm4 icons ✍
Add the following to your `build.rs`:

```toml
relm4-icons = "0.8.0"
```rust
fn main() {
relm4_icons_build::bundle_icons(
"icon_names.rs",
Some("com.example.myapp"),
Some("icons"),
None::<&str>,
[
"ssd",
"size-horizontally",
"cross",
],
);
}
```

### 4. Load the icons 🛫

Add this to your initialization code:

```rust
relm4_icons::initialize_icons();
mod icon_names {
include!(concat!(env!("OUT_DIR"), "/icon_names.rs"));
pub(crate) const GRESOURCE_BYTES: &'static [u8] = include_bytes!(concat!(env!("OUT_DIR"), "/resources.gresource"));
}

fn main() {
///...///
relm4_icons::initialize_icons(icon_names::GRESOURCE_BYTES, icon_names::RESOURCE_PREFIX);
}
```

### 5. Use the icons 🎉
Expand All @@ -78,7 +91,7 @@ button.set_icon_name("plus");
You can also use the `icon_names` module for extra compile-time generated icon names.

```rust
use relm4_icons::icon_names;
use crate::icon_names;

let button = gtk::Button::default();
button.set_icon_name(icon_names::PLUS);
Expand Down
176 changes: 0 additions & 176 deletions build.rs

This file was deleted.

15 changes: 15 additions & 0 deletions build_icons/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "relm4-icons-build"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0 OR MIT"
description = "Build icons for relm4-icons"
keywords = ["gui", "gtk", "gtk4", "elm"]
categories = ["gui"]
readme = "README.md"
repository = "https://github.com/Relm4/icons"

[dependencies]
gvdb = { version = "0.5.3", features = ["gresource"] }
serde = { version = "1.0.197", features = ["derive"] }
toml = { version = "0.8.11" }
38 changes: 38 additions & 0 deletions build_icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## relm4-icons-build

This crate provides the build-time functionality to bundle icons into the binary. It is used in the `build.rs` file of the project that uses `relm4-icons`.

### Usage

Add this to your `Cargo.toml`:

```toml
[build-dependencies]
relm4-icons-build = { version = "0.8.0" }
```

And in your `build.rs` file, use `relm4-icons-build` to bundle the icons and include them in the compiled binary:

```rust
fn main() {
relm4_icons_build::bundle_icons(
"icon_names.rs",
Some("com.example.myapp"),
Some("icons"),
None::<&str>,
[
"ssd",
"size-horizontally",
"cross",
],
);
}
```

And in your `main.rs` or `lib.rs` file, create a module named `icon_names`:

```rust
mod icon_names {
include!(concat!(env!("OUT_DIR"), "/icon_names.rs"));
}
```
17 changes: 17 additions & 0 deletions build_icons/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const CONSTANTS_FILE: &str = "shipped_icons.txt";

fn main() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let manifest_path = std::env::var("CARGO_MANIFEST_DIR").unwrap();

std::fs::write(
std::path::Path::new(&out_dir).join(CONSTANTS_FILE),
std::path::Path::new(&manifest_path)
.join("icons")
.canonicalize()
.unwrap()
.to_str()
.unwrap(),
)
.unwrap();
}
Loading

0 comments on commit c86eda6

Please sign in to comment.