Compare commits

...

2 Commits

Author SHA256 Message Date
kira cf916eb508 Refactoring for StageResult related to issue #1 2026-05-16 21:10:50 +02:00
kira 4000e0cfac More work on partition stage. 2026-05-13 00:24:05 +02:00
12 changed files with 343 additions and 321 deletions
+24
View File
@@ -0,0 +1,24 @@
use iced::widget::{Row, container, text};
use iced::{Alignment, Color, Element, Length};
// Assuming your Item struct or type implements Display or has a text representation
pub fn color_bar<Message: 'static>(
maybe_items: Option<Vec<(String, u16, Color)>>,
bar_height: u32,
) -> Element<'static, Message> {
let mut c_b = Row::new().height(bar_height);
if let Some(items) = maybe_items {
for (bar_text, bar_fill, bar_color) in items {
c_b = c_b.push(
container(text(bar_text).color(Color::BLACK).size(iced::Pixels(14.0)))
.height(Length::Fill)
.width(Length::FillPortion(bar_fill))
.style(move |_| container::background(bar_color))
.align_x(Alignment::Center)
.align_y(Alignment::Center),
);
}
}
c_b.into()
}
-76
View File
@@ -1,76 +0,0 @@
use std::fmt::Display;
use std::collections::HashMap;
use iced::widget::{Column, button, container, scrollable, text};
use iced::{Alignment, Color, Element, Length, Theme};
fn colors_map() -> HashMap<String, Color> {
HashMap::from([
("vfat".into(), Color::from_rgb8(204,121,167)),
("xfs".into(), Color::from_rgb8(0,158,115)),
("crypto_LUKS".into(), Color::from_rgb8(240,228,66)),
("swap".into(), Color::from_rgb8(213,94,0)),
("ext4".into(), Color::from_rgb8(0,114,178)),
("btrfs".into(), Color::from_rgb8(86,180,233)),
("other".into(), Color::from_rgb8(230,159,0)),
])
}
fn selected_button_style(theme: &Theme, status: button::Status) -> button::Style {
use iced::{Border, border::Radius};
//let palette = theme.palette();
let mut res_stile = button::primary(theme, button::Status::Active);
match status {
button::Status::Hovered => {
res_stile.border = Border {
color: Color::from_rgb(0.8, 0.3, 0.3),
width: 1.0,
radius: Radius::new(5.0),
};
},
_ => (),
}
return res_stile;
}
// Assuming your Item struct or type implements Display or has a text representation
pub fn list_view<T: Display>(
items: &Vec<(T, f32, Color)>,
) -> Element<'_> {
let mut column = Column::new()
.spacing(2)
.align_x(Alignment::Center)
.width(Length::Fill);
for (index, value) in items.iter().enumerate() {
// Create a row for each item, possibly with buttons or other controls
let list_item = if let Some(id) = selected_id
&& id == index
{
button(text(value.to_string())).style(selected_button_style)
} else {
button(text(value.to_string())).style(unselected_button_style)
};
let list_item = list_item
.width(Length::Fill)
.on_press(ListViewMessage::Select(index));
column = column.push(list_item);
}
// Wrap the column in a scrollable widget
container(scrollable(column).width(Length::Fill).spacing(5))
.style(container::bordered_box)
.height(container_height)
.padding(5)
.into()
}
+4 -1
View File
@@ -35,5 +35,8 @@
"partition.select_swapmode_placeholder": "Select Swap Mode",
"partition.select_swapmode": "Select swap partition mode",
"partition.use_zram": "Use zram technology",
"partition.use_secure_boot": "Setup secure boot"
"partition.use_secure_boot": "Setup secure boot",
"partition.current_dev_layout": "Current device layout",
"partition.current_after_install": "After installation device will look like this"
}
+1 -1
View File
@@ -34,7 +34,7 @@ use crate::stages::welcome::WelcomeStage;
rust_i18n::i18n!("src/locales", fallback = "en");
mod kira_theming;
mod kira_scroll_list;
//mod kira_disk_layout;
mod kira_color_bar;
mod stage;
mod stages;
+99 -31
View File
@@ -1,23 +1,22 @@
// <Kira Installer - universal Linux installer.>
// Copyright (C) <2026> <Kira Foundation>
// <Kira Installer - universal Linux installer.>
// Copyright (C) <2026> <Kira Foundation>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/*
This file contains basic enums and structs to be used with stages.
*/
*/
use std::collections::HashMap;
@@ -29,15 +28,98 @@ pub enum ConfigValue {
F64(f64),
Bool(bool),
Vector(Vec<ConfigValue>),
Dictionary(HashMap<String, ConfigValue>)
Dictionary(HashMap<String, ConfigValue>),
}
#[derive(Debug, Clone)]
pub struct StageResult {
pub name: String,
pub config: Option<HashMap<String, ConfigValue>>,
pub resuts: Option<HashMap<String, ConfigValue>>,
pub error: Option<String>
pub error: Option<String>,
}
impl StageResult {
pub fn new(name: &str) -> Self {
Self {
name: name.into(),
config: None,
error: None,
}
}
pub fn add_error(mut self, ex: String) -> Self {
self.error = Some(ex);
self
}
pub fn add_val(mut self, val_name: &str, conf_val: ConfigValue) -> Self {
match &mut self.config {
Some(c) => {
c.insert(val_name.into(), conf_val);
}
None => {
self.config = Some(HashMap::from([(val_name.into(), conf_val)]));
}
}
self
}
pub fn add_val_string(self, val_name: &str, v: String) -> Self {
self.add_val(val_name, ConfigValue::String(v))
}
pub fn add_val_bool(self, val_name: &str, v: bool) -> Self {
self.add_val(val_name, ConfigValue::Bool(v))
}
pub fn get_val(&self, val_name: &str) -> Option<ConfigValue> {
let v = self
.config
.as_ref()
.and_then(|hm| hm.get(val_name).cloned());
return v;
}
pub fn get_val_str(&self, val_name: &str) -> Option<String> {
match self.get_val(val_name) {
Some(ConfigValue::String(v)) => Some(v),
_ => None,
}
}
pub fn get_val_bool(&self, val_name: &str) -> Option<bool> {
match self.get_val(val_name) {
Some(ConfigValue::Bool(v)) => Some(v),
_ => None,
}
}
}
pub struct KiraConfig {
pub config_trail: Vec<StageResult>,
}
impl Default for KiraConfig {
fn default() -> Self {
Self {
config_trail: Vec::new(),
}
}
}
impl KiraConfig {
pub fn get_stage(&self, name: &str) -> Option<StageResult> {
self.config_trail
.iter()
.find(|v| v.name == name)
.and_then(|v| Some(v.clone()))
}
pub fn pop_last(&mut self) -> Option<StageResult> {
self.config_trail.pop()
}
pub fn push(&mut self, stage_result: StageResult) {
self.config_trail.push(stage_result);
}
}
#[derive(Debug, Clone)]
@@ -47,17 +129,3 @@ pub enum StageAction {
Next(StageResult),
Abort,
}
pub struct KiraConfig {
pub config_trail: Vec<StageResult>,
}
impl KiraConfig {
pub fn get_stage(&self, name: &str) -> Option<StageResult> {
self.config_trail.iter().find(|v| v.name == name)
.and_then(|v| Some(v.clone()))
}
}
+5 -18
View File
@@ -276,25 +276,12 @@ impl KeyboardStage {
if let Some(model) = &self.model
&& let Some(k_l) = &self.keyboard_layout
{
StageResult {
name: "keyboard".to_string(),
config: Some(HashMap::from([
(
"model".to_string(),
ConfigValue::String(model.model.clone()),
),
("layout".to_string(), ConfigValue::String(k_l.to_result())),
])),
resuts: None,
error: None,
}
StageResult::new("keyboard")
.add_val_string("model", model.model.clone())
.add_val_string("layout", k_l.to_result())
} else {
StageResult {
name: "keyboard".to_string(),
config: None,
resuts: None,
error: Some("Something go terrible wrong, there is no keyboard layouts.".into()),
}
StageResult::new("keyboard")
.add_error("Something go terrible wrong, there is no keyboard layouts.".into())
}
}
+2 -10
View File
@@ -21,7 +21,6 @@
use iced::{Alignment, widget};
use rust_i18n::t;
use std::collections::HashMap;
use crate::stage;
use crate::stage::StageResult;
@@ -44,15 +43,8 @@ pub enum Message {
impl LicenseStage {
fn accepted() -> StageResult {
StageResult {
name: "license".into(),
config: Some(HashMap::from([(
"accepted".to_string(),
stage::ConfigValue::Bool(true),
)])),
resuts: None,
error: None,
}
StageResult::new("license")
.add_val_bool("accepted", true)
}
pub fn update(&mut self, message: Message) -> stage::StageAction {
+7 -27
View File
@@ -215,35 +215,15 @@ impl LocaleStage {
if let Some(lang_locale) = &self.lang_locale
&& let Some(formats_locale) = &self.formats_locale
{
StageResult {
name: "locale".to_string(),
config: Some(HashMap::from([
(
"lang_locale".to_string(),
ConfigValue::String(lang_locale.code.clone()),
),
(
"formats_locale".to_string(),
ConfigValue::String(formats_locale.code.clone()),
),
])),
resuts: None,
error: None,
}
StageResult::new("locale")
.add_val_string("lang_locale", lang_locale.code.clone())
.add_val_string("formats_locale", formats_locale.code.clone())
} else {
let loc = LocaleData::default();
StageResult {
name: "locale".to_string(),
config: Some(HashMap::from([
(
"lang_locale".to_string(),
ConfigValue::String(loc.code.clone()),
),
("formats_locale".to_string(), ConfigValue::String(loc.code)),
])),
resuts: None,
error: None,
}
StageResult::new("locale")
.add_val_string("lang_locale", loc.code.clone())
.add_val_string("formats_locale", loc.code)
}
}
+3 -15
View File
@@ -127,21 +127,9 @@ impl NetworkStage {
}
fn gen_result(&self) -> StageResult {
StageResult {
name: "network".into(),
config: Some(HashMap::from([
(
"internet_active".to_string(),
stage::ConfigValue::Bool(self.internet_active),
),
(
"use_net_settings".to_string(),
stage::ConfigValue::Bool(self.use_net_settings),
),
])),
resuts: None,
error: None,
}
StageResult::new("network")
.add_val_bool("internet_active", self.internet_active)
.add_val_bool("use_net_settings", self.use_net_settings)
}
pub fn update(&mut self, message: Message) -> UpdateResult {
+177 -77
View File
@@ -20,7 +20,8 @@
use crate::stage::{ConfigValue, StageAction, StageResult};
use iced::{Alignment, Color, Length, widget::{self, container}};
use crate::kira_color_bar;
use iced::{Alignment, Color, Length, widget};
use rust_i18n::t;
use std::collections::HashMap;
@@ -55,7 +56,7 @@ pub struct BlkDev {
impl std::fmt::Display for BlkDev {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({}MB)", &self.name, self.size_mb)
write!(f, "{} ({})", &self.name, size_mb_to_text(self.size_mb))
}
}
@@ -70,6 +71,76 @@ impl BlkDev {
}
}
fn part_type_to_color(t: &str) -> Color {
match t {
"vfat" => Color::from_rgb8(204, 121, 167),
"xfs" => Color::from_rgb8(0, 158, 115),
"crypto_LUKS" => Color::from_rgb8(240, 228, 66),
"swap" => Color::from_rgb8(213, 94, 0),
"ext4" => Color::from_rgb8(0, 114, 178),
"btrfs" => Color::from_rgb8(86, 180, 233),
_ => Color::from_rgb8(230, 159, 0),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartInfo {
name: String,
part_type: String,
size_mb: u64,
}
fn size_mb_to_text(size_mb: u64) -> String {
let mut s = size_mb;
if s < 1000 {
format!("{}MB", s)
} else {
s = s / 1024;
if s < 1000 {
format!("{}GB", s)
} else {
s = s / 1024;
format!("{}TB", s)
}
}
}
impl std::fmt::Display for PartInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {}",
&self.name,
self.part_type,
size_mb_to_text(self.size_mb)
)
}
}
impl PartInfo {
pub fn from_vect(v: &Vec<String>) -> Option<Self> {
if v.len() == 4 && v[3] == "part" {
let name = v[0].clone();
let size_mb = u64::from_str_radix(&v[1], 10).unwrap_or(0) / 1048576;
let part_type = v[2].clone();
Some(Self {
name: name,
part_type: part_type,
size_mb: size_mb,
})
} else {
None
}
}
pub fn to_ct_params(&self, dev_size_mb: u64) -> (String, u16, Color) {
let fill_ratio: u16 = ((self.size_mb as f32 / dev_size_mb as f32) * 12.0) as u16;
let fill_ratio = fill_ratio.max(1);
let part_color = part_type_to_color(&self.part_type);
(self.to_string(), fill_ratio, part_color)
}
}
#[derive(Debug, Clone)]
pub struct PartitionStage {
device: Option<BlkDev>,
@@ -77,8 +148,7 @@ pub struct PartitionStage {
swap_mode: Option<SwapMode>,
use_zram: bool,
secure_boot: bool,
min_disk_size_mb: u64,
selected_disk_partitions: Option<Vec<(String, u16, Color)>>,
selected_disk_parts: Option<Vec<(String, u16, Color)>>,
}
#[derive(Debug, Clone)]
@@ -125,21 +195,23 @@ pub fn get_disks_info_blocking() -> Result<Vec<BlkDev>, String> {
}
}
pub fn get_dev_part_blocking(dev_name: &str) -> Result<Vec<(String, String, u64)>, String> {
fn part_from_vect(v: &Vec<String>) -> (String, String, u64) {
let name = v[0].clone();
let size_mb = u64::from_str_radix(&v[1], 10).unwrap_or(0) / 1048576;
let part_type = v[2].clone();
(name, part_type, size_mb)
}
/// getting list of device partitions
pub fn get_dev_part_blocking(dev_name: &str) -> Result<Vec<PartInfo>, String> {
use std::process::Command;
//"lsblk -o NAME,SIZE,FSTYPE -l -n /dev/nvme0n1"
match Command::new("lsblk").args(["-o", "NAME,SIZE,FSTYPE,TYPE", "-l", "-n", "-b", format!("/dev/{}", dev_name).as_str()]).output() {
match Command::new("lsblk")
.args([
"-o",
"NAME,SIZE,FSTYPE,TYPE",
"-l",
"-n",
"-b",
format!("/dev/{}", dev_name).as_str(),
])
.output()
{
Ok(cmd_output) => {
if cmd_output.status.success() {
match String::from_utf8(cmd_output.stdout) {
@@ -150,9 +222,7 @@ pub fn get_dev_part_blocking(dev_name: &str) -> Result<Vec<(String, String, u64)
.map(str::to_string)
.collect::<Vec<String>>()
})
.map(|v| {println!("{:?}", &v); v})
.filter(|l_v| l_v.len() ==4 && l_v[3] == "part")
.map(|l_v| part_from_vect(&l_v))
.filter_map(|v| PartInfo::from_vect(&v))
.collect()),
Err(ex) => Err(format!(
"Exception while converting partition info to UTF8 {}",
@@ -170,20 +240,52 @@ pub fn get_dev_part_blocking(dev_name: &str) -> Result<Vec<(String, String, u64)
}
}
fn part_colors_map() -> HashMap<String, Color> {
HashMap::from([
("vfat".into(), Color::from_rgb8(204,121,167)),
("xfs".into(), Color::from_rgb8(0,158,115)),
("crypto_LUKS".into(), Color::from_rgb8(240,228,66)),
("swap".into(), Color::from_rgb8(213,94,0)),
("ext4".into(), Color::from_rgb8(0,114,178)),
("btrfs".into(), Color::from_rgb8(86,180,233)),
("other".into(), Color::from_rgb8(230,159,0)),
])
}
use toml::Table;
impl PartitionStage {
pub fn gen_disk_layout_estimate(&self) -> Vec<PartInfo> {
let dev = self.device.clone().unwrap();
let swap_size: u64 = match self.swap_mode {
Some(SwapMode::NoSwap) => 0,
Some(SwapMode::SwapHibernate) => 666,
Some(SwapMode::SwapNoHibernate) => 1024,
_ => 1024,
};
let mut res: Vec<PartInfo> = Vec::new();
// UEFI partition
res.push(PartInfo {
name: "EFI".into(),
part_type: "vfat".into(),
size_mb: 256,
});
// boot A and B
res.push(PartInfo {
name: "kira_boot_a".into(),
part_type: "xfs".into(),
size_mb: 512,
});
res.push(PartInfo {
name: "kira_boot_b".into(),
part_type: "xfs".into(),
size_mb: 512,
});
// user data
res.push(PartInfo {
name: "kira_data".into(),
part_type: "xfs".into(),
size_mb: dev.size_mb - 1024 - 256 - swap_size,
});
// swap
if swap_size > 0 {
res.push(PartInfo {
name: "".into(),
part_type: "swap".into(),
size_mb: swap_size,
});
}
res
}
pub fn new(toml_config: &Table) -> Result<Self, String> {
let maybe_dev_list = get_disks_info_blocking();
@@ -210,8 +312,7 @@ impl PartitionStage {
swap_mode: Some(SwapMode::SwapNoHibernate),
use_zram: true,
secure_boot: false,
min_disk_size_mb: min_disk_size_mb,
selected_disk_partitions: None,
selected_disk_parts: None,
})
}
Err(ex) => Err(ex),
@@ -219,49 +320,35 @@ impl PartitionStage {
}
fn gen_result(&self) -> StageResult {
StageResult {
name: "partition".to_string(),
config: Some(HashMap::from([
(
"device".to_string(),
ConfigValue::String(
StageResult::new("partition")
.add_val_string(
"device",
self.device
.as_ref()
.and_then(|v| Some(v.name.clone()))
.unwrap_or_else(|| "NONE".to_string()),
),
),
(
"swap_mode".to_string(),
ConfigValue::String(
)
.add_val_string(
"swap_mode",
self.swap_mode
.clone()
.unwrap_or(SwapMode::NoSwap)
.to_string(),
),
),
("zram".to_string(), ConfigValue::Bool(self.use_zram)),
(
"secure_boot".to_string(),
ConfigValue::Bool(self.secure_boot),
),
])),
resuts: None,
error: None,
}
)
.add_val_bool("zram", self.use_zram)
.add_val_bool("secure_boot", self.secure_boot)
}
pub fn update(&mut self, message: Message) -> StageAction {
match message {
Message::SelectDevice(dev) => {
self.selected_disk_partitions = match get_dev_part_blocking(&dev.name) {
Ok(dev_parts) => Some(dev_parts.iter().map(|(p_name, p_type, p_size)| {
let fill_ratio: u16 = ((*p_size as f32 / dev.size_mb as f32) * 15.0) as u16;
let fill_ratio = fill_ratio.max(2);
let p_clor = part_colors_map()[p_type];
let p_text = format!("{} {} {}", p_name, p_type, p_size);
(p_text, fill_ratio, p_clor)}).collect()),
self.selected_disk_parts = match get_dev_part_blocking(&dev.name) {
Ok(dev_parts) => Some(
dev_parts
.iter()
.map(|part_inf| part_inf.to_ct_params(dev.size_mb))
.collect(),
),
Err(ex) => {
println!("Error getting dev partitions info: {}", ex);
None
@@ -289,22 +376,34 @@ impl PartitionStage {
}
pub fn view(&self) -> iced::Element<'_, Message> {
let dev_parts_es: Option<Vec<(String, u16, Color)>> = if self.selected_disk_parts.is_some()
{
Some(
self.gen_disk_layout_estimate()
.iter()
.map(|part_inf| part_inf.to_ct_params(self.device.clone().unwrap().size_mb))
.collect(),
)
} else {
None
};
let dev_parts_text = match dev_parts_es {
Some(_) => widget::text(t!("partition.current_after_install")),
None => widget::text(""),
};
let dev_parts_content = kira_color_bar::color_bar(dev_parts_es, 48);
let dev_parts_content = widget::column![dev_parts_text, dev_parts_content];
let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back);
let next_button = widget::button(widget::text(t!("button.next"))).on_press(Message::Next);
let mut selected_dev_content = widget::Row::new().height(48);
if let Some(dev_cont) = &self.selected_disk_partitions {
for part_info in dev_cont {
selected_dev_content = selected_dev_content.push(
widget::container(widget::text(&part_info.0).color(Color::BLACK))
.height(Length::Fill).width(Length::FillPortion(part_info.1))
.style(|_| container::background(part_info.2))
.align_x(Alignment::Center).align_y(Alignment::Center)
);
}
}
let selected_dev_text = match self.selected_disk_parts {
Some(_) => widget::text(t!("partition.current_dev_layout")),
None => widget::text(""),
};
let selected_dev_content = kira_color_bar::color_bar(self.selected_disk_parts.clone(), 48);
let selected_dev_content = widget::column![selected_dev_text, selected_dev_content];
widget::column![
widget::container(
@@ -333,7 +432,8 @@ impl PartitionStage {
widget::checkbox(self.secure_boot)
.label(t!("partition.use_secure_boot"))
.on_toggle(Message::ToggleSecureBoot),
widget::container(selected_dev_content).style(widget::container::bordered_box),
selected_dev_content,
dev_parts_content,
]
.padding(10)
.spacing(10)
@@ -341,7 +441,7 @@ impl PartitionStage {
)
.height(iced::Length::Fill)
.width(iced::Length::Fill)
.align_x(Alignment::Center)
.align_x(Alignment::Start)
.align_y(Alignment::Center),
widget::row![
back_button,
+5 -25
View File
@@ -146,32 +146,12 @@ impl TimeZoneStage {
fn gen_result(&self) -> StageResult {
if let Some(time_zone) = &self.selected_zone {
StageResult {
name: "time_zone".to_string(),
config: Some(HashMap::from([
(
"name".to_string(),
ConfigValue::String(time_zone.to_string()),
),
(
"region".to_string(),
ConfigValue::String(time_zone.region.clone()),
),
(
"zone".to_string(),
ConfigValue::String(time_zone.zone.clone()),
),
])),
resuts: None,
error: None,
}
StageResult::new("time_zone")
.add_val_string("name", time_zone.to_string())
.add_val_string("region", time_zone.region.clone())
.add_val_string("zone", time_zone.zone.clone())
} else {
StageResult {
name: "time_zone".to_string(),
config: None,
resuts: None,
error: None,
}
StageResult::new("time_zone")
}
}
+6 -30
View File
@@ -70,37 +70,13 @@ impl WelcomeStage {
fn gen_result(&self) -> StageResult {
if let Some(loc) = &self.locale {
StageResult {
name: "welcome".to_string(),
config: Some(HashMap::from([
(
"loc_code".to_string(),
ConfigValue::String(loc.code.clone()),
),
(
"loc_name".to_string(),
ConfigValue::String(loc.name.clone()),
),
])),
resuts: None,
error: None,
}
StageResult::new("welcome")
.add_val_string("loc_code", loc.code.clone())
.add_val_string("loc_name", loc.name.clone())
} else {
StageResult {
name: "welcome".to_string(),
config: Some(HashMap::from([
(
"loc_code".to_string(),
ConfigValue::String("en".to_string()),
),
(
"loc_name".to_string(),
ConfigValue::String("English".to_string()),
),
])),
resuts: None,
error: None,
}
StageResult::new("welcome")
.add_val_string("loc_code", "en".to_string())
.add_val_string("loc_name", "English".to_string())
}
}