Support for commenting on posts

This commit is contained in:
Bnyro
2023-06-20 12:20:28 +02:00
parent 99f35bfe7a
commit 10b5059420
4 changed files with 99 additions and 29 deletions

View File

@@ -2,11 +2,20 @@ use relm4::{prelude::*, MessageBroker};
use gtk::prelude::*;
pub static CREATE_POST_DIALOG_BROKER: MessageBroker<DialogMsg> = MessageBroker::new();
pub static CREATE_COMMENT_DIALOG_BROKER: MessageBroker<DialogMsg> = MessageBroker::new();
pub struct CreatePostDialog {
type_: DialogType,
visible: bool,
}
#[derive(Debug, Clone, Copy)]
pub enum DialogType {
Post,
Comment
}
#[derive(Debug)]
pub enum DialogMsg {
Show,
@@ -16,12 +25,12 @@ pub enum DialogMsg {
#[derive(Debug)]
pub enum CreatePostDialogOutput {
CreatePostRequest(String, String)
CreateRequest(String, String)
}
#[relm4::component(pub)]
impl SimpleComponent for CreatePostDialog {
type Init = ();
type Init = DialogType;
type Input = DialogMsg;
type Output = CreatePostDialogOutput;
@@ -38,24 +47,43 @@ impl SimpleComponent for CreatePostDialog {
set_width_request: 600,
set_margin_all: 20,
gtk::Label {
set_halign: gtk::Align::Center,
set_valign: gtk::Align::Center,
set_label: "Create post",
add_css_class: "font-bold"
},
#[name(name)]
gtk::Entry {
set_placeholder_text: Some("Title"),
set_margin_top: 10,
set_margin_bottom: 10,
match model.type_ {
DialogType::Post => {
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
gtk::Label {
set_halign: gtk::Align::Center,
set_valign: gtk::Align::Center,
set_label: "Create post",
add_css_class: "font-bold"
},
#[name(name)]
gtk::Entry {
set_placeholder_text: Some("Title"),
set_margin_top: 10,
set_margin_bottom: 10,
},
}
}
DialogType::Comment => {
gtk::Box {
gtk::Label {
set_halign: gtk::Align::Center,
set_valign: gtk::Align::Center,
set_label: "Create comment",
add_css_class: "font-bold"
},
}
}
},
gtk::Label {
set_text: "Body:",
set_halign: gtk::Align::Start,
},
#[name(body)]
gtk::TextView {
set_editable: true,
set_margin_top: 5,
set_margin_bottom: 10,
set_vexpand: true,
},
@@ -74,8 +102,16 @@ impl SimpleComponent for CreatePostDialog {
let body_buffer = body.buffer();
let (start, end) = &body_buffer.bounds();
let body = body_buffer.text(start, end, true).to_string();
if name.is_empty() || body.is_empty() { return; }
sender.input(DialogMsg::Okay(name, body))
match model.type_ {
DialogType::Post => {
if name.is_empty() || body.is_empty() { return; }
sender.input(DialogMsg::Okay(name, body))
}
DialogType::Comment => {
if name.is_empty() { return; }
sender.input(DialogMsg::Okay(name, body))
}
}
},
}
}
@@ -89,11 +125,11 @@ impl SimpleComponent for CreatePostDialog {
}
fn init(
_init: Self::Init,
init: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = CreatePostDialog { visible: false };
let model = CreatePostDialog { type_: init, visible: false };
let widgets = view_output!();
ComponentParts { model, widgets }
}
@@ -103,7 +139,7 @@ impl SimpleComponent for CreatePostDialog {
DialogMsg::Show => self.visible = true,
DialogMsg::Hide => self.visible = false,
DialogMsg::Okay(name, body) => {
let _ = sender.output(CreatePostDialogOutput::CreatePostRequest(name, body));
let _ = sender.output(CreatePostDialogOutput::CreateRequest(name, body));
self.visible = false;
}
}