mirror of https://github.com/TaKO8Ki/gobang
Implement TableValue component (#17)
* use table value component * add todo * add linux target_os * add default features for copypasta * fix clippy warnings * update gobang.gifpull/19/head
parent
0be60b5039
commit
0ebb39f3cd
Binary file not shown.
Before Width: | Height: | Size: 4.5 MiB After Width: | Height: | Size: 4.1 MiB |
@ -0,0 +1,75 @@
|
||||
#[cfg(any(test, not(any(target_os = "linux", target_os = "macos", windows))))]
|
||||
use copypasta::nop_clipboard::NopClipboardContext;
|
||||
#[cfg(target_os = "linux")]
|
||||
use copypasta::x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext};
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", windows))]
|
||||
use copypasta::ClipboardContext;
|
||||
use copypasta::ClipboardProvider;
|
||||
|
||||
pub struct Clipboard {
|
||||
clipboard: Box<dyn ClipboardProvider>,
|
||||
selection: Option<Box<dyn ClipboardProvider>>,
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
#[cfg(any(target_os = "linux", target_os = "macos", windows))]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[cfg(any(test, not(any(target_os = "linux", target_os = "macos", windows))))]
|
||||
pub fn new_nop() -> Self {
|
||||
Self {
|
||||
clipboard: Box::new(NopClipboardContext::new().unwrap()),
|
||||
selection: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Clipboard {
|
||||
fn default() -> Self {
|
||||
#[cfg(any(target_os = "macos", windows))]
|
||||
return Self {
|
||||
clipboard: Box::new(ClipboardContext::new().unwrap()),
|
||||
selection: None,
|
||||
};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
return Self {
|
||||
clipboard: Box::new(ClipboardContext::new().unwrap()),
|
||||
selection: Some(Box::new(
|
||||
X11ClipboardContext::<X11SelectionClipboard>::new().unwrap(),
|
||||
)),
|
||||
};
|
||||
|
||||
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
|
||||
return Self::new_nop();
|
||||
}
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
pub fn store(&mut self, text: impl Into<String>) {
|
||||
let clipboard = match &mut self.selection {
|
||||
Some(provider) => provider,
|
||||
None => &mut self.clipboard,
|
||||
};
|
||||
|
||||
clipboard.set_contents(text.into()).unwrap_or_else(|err| {
|
||||
panic!("Unable to store text in clipboard: {}", err);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn _load(&mut self) -> String {
|
||||
let clipboard = match &mut self.selection {
|
||||
Some(provider) => provider,
|
||||
None => &mut self.clipboard,
|
||||
};
|
||||
|
||||
match clipboard.get_contents() {
|
||||
Err(err) => {
|
||||
panic!("Unable to load text from clipboard: {}", err);
|
||||
}
|
||||
Ok(text) => text,
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
use super::{Component, DrawableComponent};
|
||||
use crate::event::Key;
|
||||
use anyhow::Result;
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::{Alignment, Rect},
|
||||
style::{Color, Modifier, Style},
|
||||
text::Span,
|
||||
widgets::{Block, Borders, Paragraph, Wrap},
|
||||
Frame,
|
||||
};
|
||||
|
||||
pub struct TableValueComponent {
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
impl TableValueComponent {
|
||||
pub fn new(value: String) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawableComponent for TableValueComponent {
|
||||
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect, focused: bool) -> Result<()> {
|
||||
let paragraph = Paragraph::new(self.value.clone())
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default())
|
||||
.title(Span::styled(
|
||||
"Value",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
)),
|
||||
)
|
||||
.style(if focused {
|
||||
Style::default()
|
||||
} else {
|
||||
Style::default().fg(Color::DarkGray)
|
||||
})
|
||||
.alignment(Alignment::Left)
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(paragraph, area);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for TableValueComponent {
|
||||
fn event(&mut self, _key: Key) -> Result<()> {
|
||||
todo!("scroll");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue