use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use gtk::prelude::*;
use log::{debug, error, warn};
use nvim_rs::{Tabpage, Window as NvimWindow};
use crate::nvim_bridge::{
CmdlineBlockAppend, CmdlineBlockShow, CmdlinePos, CmdlineShow,
CmdlineSpecialChar, DefaultColorsSet, GnvimEvent, GridCursorGoto,
GridLineSegment, GridResize, GridScroll, HlAttrDefine, HlGroupSet,
ModeChange, ModeInfo, ModeInfoSet, MsgSetPos, Notify, OptionSet,
PopupmenuShow, RedrawEvent, TablineUpdate, WindowExternalPos,
WindowFloatPos, WindowPos,
};
use crate::nvim_gio::GioNeovim;
use crate::ui::cmdline::Cmdline;
use crate::ui::color::{HlDefs, HlGroup};
use crate::ui::common::spawn_local;
#[cfg(feature = "libwebkit2gtk")]
use crate::ui::cursor_tooltip::{CursorTooltip, Gravity};
use crate::ui::font::Font;
use crate::ui::grid::{Grid, GridMetrics};
use crate::ui::popupmenu::Popupmenu;
use crate::ui::tabline::Tabline;
use crate::ui::window::{MsgWindow, Window};
pub(crate) type Windows = HashMap<i64, Window>;
pub(crate) type Grids = HashMap<i64, Grid>;
pub(crate) struct ResizeOptions {
pub font: Font,
pub line_space: i64,
}
/// Internal structure for `UI` to work on.
pub(crate) struct UIState {
pub css_provider: gtk::CssProvider,
pub windows: Windows,
/// Container for non-floating windows.
pub windows_container: gtk::Fixed,
/// Container for floating windows.
pub windows_float_container: gtk::Fixed,
/// Container for the msg window/grid.
pub msg_window_container: gtk::Fixed,
/// Window for our messages grid.
pub msg_window: MsgWindow,
/// All grids currently in the UI.
pub grids: Grids,
/// Highlight definitions.
pub hl_defs: HlDefs,
/// Mode infos. When a mode is activated, the activated mode is passed
/// to the gird(s).
pub mode_infos: Vec<ModeInfo>,
/// Current mode.
pub current_mode: Option<ModeInfo>,
/// Id of the current active grid.
pub current_grid: i64,
pub popupmenu: Popupmenu,
pub cmdline: Cmdline,
pub tabline: Tabline,
#[cfg(feature = "libwebkit2gtk")]
pub cursor_tooltip: CursorTooltip,
pub wildmenu_shown: bool,
/// Overlay contains our grid(s) and popupmenu.
#[allow(unused)]
pub overlay: gtk::Overlay,
/// Source id for delayed call to ui_try_resize.
pub resize_source_id: Rc<RefCell<Option<glib::SourceId>>>,
/// Resize options that is some if a resize should be send to nvim on flush.
pub resize_on_flush: Option<ResizeOptions>,
/// Flag for flush to update GUI colors on components that depend on
/// highlight defs and groups.
pub hl_changed: bool,
pub font: Font,
pub line_space: i64,
pub enable_cursor_animations: bool,
}
impl UIState {
pub fn handle_notify(
&mut self,
window: >k::ApplicationWindow,
notify: Notify,
nvim: &GioNeovim,
) {
match notify {
Notify::RedrawEvent(events) => {
events.into_iter().for_each(|e| {
self.handle_redraw_event(window, e, &nvim);
});
}
Notify::GnvimEvent(event) => match event {
Ok(event) => self.handle_gnvim_event(&event, nvim),
Err(err) => {
let nvim = nvim.clone();
let msg = format!(
"echom \"<