| 1 | /* |
| 2 | * Copyright 2020-2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use craterust-analyzer cargo gnome-robots 50.0.0 crate/::themerust-analyzer cargo gnome-robots 50.0.0 theme/::Themerust-analyzer cargo gnome-robots 50.0.0 theme/Theme#; |
| 21 | use craterust-analyzer cargo gnome-robots 50.0.0 crate/::utilsrust-analyzer cargo gnome-robots 50.0.0 utils/::list_directoryrust-analyzer cargo gnome-robots 50.0.0 utils/list_directory().; |
| 22 | use gtkrust-analyzer cargo gtk4 0.11.1 crate/::{giorust-analyzer cargo gio 0.22.2 crate/, giorust-analyzer cargo gio 0.22.2 crate/::preluderust-analyzer cargo gio 0.22.2 prelude/::*}; |
| 23 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#; |
| 24 | |
| 25 | pub fn themes_from_directoryrust-analyzer cargo gnome-robots 50.0.0 themes/themes_from_directory().(directory: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> giorust-analyzer cargo gio 0.22.2 crate/::ListStorerust-analyzer cargo gio 0.22.2 auto/list_store/ListStore# { |
| 26 | let files = list_directoryrust-analyzer cargo gnome-robots 50.0.0 utils/list_directory().(directory).unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|err| { |
| 27 | eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!( |
| 28 | "Failed to list theme directory {}: {}", |
| 29 | directory.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), |
| 30 | err |
| 31 | ); |
| 32 | Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() |
| 33 | }); |
| 34 | |
| 35 | let themes = giorust-analyzer cargo gio 0.22.2 crate/::ListStorerust-analyzer cargo gio 0.22.2 auto/list_store/ListStore#::newrust-analyzer cargo gio 0.22.2 list_store/impl#[ListStore]new().::<Themerust-analyzer cargo gnome-robots 50.0.0 theme/Theme#>(); |
| 36 | for path in files { |
| 37 | match Themerust-analyzer cargo gnome-robots 50.0.0 theme/Theme#::from_filerust-analyzer cargo gnome-robots 50.0.0 theme/impl#[Theme]from_file().(&path) { |
| 38 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(theme) => { |
| 39 | themes.appendrust-analyzer cargo gio 0.22.2 auto/list_store/impl#[ListStore]append().(&theme); |
| 40 | } |
| 41 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => { |
| 42 | eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Failed to load theme '{}': {}", path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(), error) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | themes |
| 47 | } |
| 48 | |
| 49 | pub fn find_by_namerust-analyzer cargo gnome-robots 50.0.0 themes/find_by_name().(themes: &giorust-analyzer cargo gio 0.22.2 crate/::ListModelrust-analyzer cargo gio 0.22.2 auto/list_model/ListModel#, name: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(u32, Themerust-analyzer cargo gnome-robots 50.0.0 theme/Theme#)> { |
| 50 | let size = themes.n_itemsrust-analyzer cargo gio 0.22.2 auto/list_model/ListModelExt#n_items().(); |
| 51 | for index in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#size { |
| 52 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(theme) = themes.itemrust-analyzer cargo gio 0.22.2 auto/list_model/ListModelExt#item().(index).and_downcastrust-analyzer cargo glib 0.22.3 object/impl#[`Option<I>`][CastNone]and_downcast().::<Themerust-analyzer cargo gnome-robots 50.0.0 theme/Theme#>() else { |
| 53 | continue; |
| 54 | }; |
| 55 | if theme.namerust-analyzer cargo gnome-robots 50.0.0 theme/imp/impl#[`super::Theme`]name().() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().name || theme.pathrust-analyzer cargo gnome-robots 50.0.0 theme/imp/impl#[`super::Theme`]path().().file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().().is_some_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some_and().(|n| n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().name) { |
| 56 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((index, theme)); |
| 57 | } |
| 58 | } |
| 59 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 60 | } |
| 61 | |
| 62 | const DEFAULT_THEMErust-analyzer cargo gnome-robots 50.0.0 themes/DEFAULT_THEME.: &str = "robots.svg"; |
| 63 | |
| 64 | pub fn find_best_matchrust-analyzer cargo gnome-robots 50.0.0 themes/find_best_match().(themes: &giorust-analyzer cargo gio 0.22.2 crate/::ListModelrust-analyzer cargo gio 0.22.2 auto/list_model/ListModel#, name: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Themerust-analyzer cargo gnome-robots 50.0.0 theme/Theme#, &'static str> { |
| 65 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_, theme)) = find_by_namerust-analyzer cargo gnome-robots 50.0.0 themes/find_by_name().(themes, name) { |
| 66 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(theme); |
| 67 | } |
| 68 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((_, theme)) = find_by_namerust-analyzer cargo gnome-robots 50.0.0 themes/find_by_name().(themes, DEFAULT_THEMErust-analyzer cargo gnome-robots 50.0.0 themes/DEFAULT_THEME.) { |
| 69 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(theme); |
| 70 | } |
| 71 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("No theme was found.") |
| 72 | } |