2024-03-29 21:48:44 +00:00
use super ::{ BarConfig , BarPosition , MonitorConfig } ;
2022-12-15 21:37:08 +00:00
use color_eyre ::{ Help , Report } ;
2022-10-15 16:27:25 +01:00
use gtk ::Orientation ;
2022-12-12 23:18:22 +00:00
use serde ::{ Deserialize , Deserializer } ;
2022-08-14 14:30:13 +01:00
2022-12-12 23:18:22 +00:00
// Manually implement for better untagged enum error handling:
// currently open pr: https://github.com/serde-rs/serde/pull/1544
impl < ' de > Deserialize < ' de > for MonitorConfig {
2023-03-19 16:17:31 +00:00
fn deserialize < D > ( deserializer : D ) -> Result < Self , D ::Error >
2022-12-12 23:18:22 +00:00
where
D : Deserializer < ' de > ,
{
let content =
< serde ::__private ::de ::Content as serde ::Deserialize > ::deserialize ( deserializer ) ? ;
2024-03-29 21:48:44 +00:00
match < BarConfig as serde ::Deserialize > ::deserialize (
2022-12-12 23:18:22 +00:00
serde ::__private ::de ::ContentRefDeserializer ::< D ::Error > ::new ( & content ) ,
) {
Ok ( config ) = > Ok ( Self ::Single ( config ) ) ,
2024-03-29 21:48:44 +00:00
Err ( outer ) = > match < Vec < BarConfig > as serde ::Deserialize > ::deserialize (
2022-12-12 23:18:22 +00:00
serde ::__private ::de ::ContentRefDeserializer ::< D ::Error > ::new ( & content ) ,
) {
Ok ( config ) = > Ok ( Self ::Multiple ( config ) ) ,
Err ( inner ) = > {
let report = Report ::msg ( format! ( " multi-bar (c): {inner} " ) . replace ( " An error occurred when deserializing: " , " " ) )
. wrap_err ( format! ( " single-bar (b): {outer} " ) . replace ( " An error occurred when deserializing: " , " " ) )
. wrap_err ( " An invalid config was found. The following errors were encountered: " )
. note ( " Both the single-bar (type b / error 1) and multi-bar (type c / error 2) config variants were tried. You can likely ignore whichever of these is not relevant to you. " )
. suggestion ( " Please see https://github.com/JakeStanger/ironbar/wiki/configuration-guide#2-pick-your-use-case for more info on the above " ) ;
Err ( serde ::de ::Error ::custom ( format! ( " {report:?} " ) ) )
}
} ,
}
}
}
2024-06-13 21:42:40 +01:00
pub fn deserialize_layer < ' de , D > ( deserializer : D ) -> Result < gtk_layer_shell ::Layer , D ::Error >
where
D : serde ::Deserializer < ' de > ,
{
use gtk_layer_shell ::Layer ;
let value = Option ::< String > ::deserialize ( deserializer ) ? ;
value
. map ( | v | match v . as_str ( ) {
" background " = > Ok ( Layer ::Background ) ,
" bottom " = > Ok ( Layer ::Bottom ) ,
" top " = > Ok ( Layer ::Top ) ,
" overlay " = > Ok ( Layer ::Overlay ) ,
_ = > Err ( serde ::de ::Error ::custom ( " invalid value for orientation " ) ) ,
} )
. unwrap_or ( Ok ( Layer ::Top ) )
}
#[ cfg(feature = " schema " ) ]
pub fn schema_layer ( gen : & mut schemars ::gen ::SchemaGenerator ) -> schemars ::schema ::Schema {
use schemars ::JsonSchema ;
let mut schema : schemars ::schema ::SchemaObject = < String > ::json_schema ( gen ) . into ( ) ;
schema . enum_values = Some ( vec! [
" background " . into ( ) ,
" bottom " . into ( ) ,
" top " . into ( ) ,
" overlay " . into ( ) ,
] ) ;
schema . into ( )
}
2022-10-15 16:27:25 +01:00
impl BarPosition {
2022-12-11 21:31:45 +00:00
/// Gets the orientation the bar and widgets should use
/// based on this position.
2024-02-18 14:54:17 +00:00
pub fn orientation ( self ) -> Orientation {
2022-10-15 16:27:25 +01:00
if self = = Self ::Top | | self = = Self ::Bottom {
Orientation ::Horizontal
} else {
Orientation ::Vertical
}
}
2022-12-11 21:31:45 +00:00
/// Gets the angle that label text should be displayed at
/// based on this position.
2022-10-15 16:27:25 +01:00
pub const fn get_angle ( self ) -> f64 {
match self {
Self ::Top | Self ::Bottom = > 0.0 ,
Self ::Left = > 90.0 ,
Self ::Right = > 270.0 ,
}
}
}