Rework resize handling

Fixes #383
This commit is contained in:
Maxime Coste 2015-08-23 15:18:18 +01:00
parent a2e90c2c25
commit 0755366cb1
6 changed files with 20 additions and 30 deletions

View File

@ -73,6 +73,8 @@ void Client::handle_available_input(EventMode mode)
context().hooks().run_hook("FocusIn", context().name(), context());
else if (*key == Key::FocusOut)
context().hooks().run_hook("FocusOut", context().name(), context());
else if (key->modifiers == Key::Modifiers::Resize)
force_redraw();
else
m_input_handler.handle_key(*key);
}

View File

@ -78,7 +78,7 @@ struct MouseHandler
{
case Key::Modifiers::MousePress:
m_dragging = true;
m_anchor = context.window().buffer_coord(key.mouse_coord());
m_anchor = context.window().buffer_coord(key.coord());
context.selections_write_only() = SelectionList{ buffer, m_anchor };
return true;
@ -86,7 +86,7 @@ struct MouseHandler
if (not m_dragging)
return true;
m_dragging = false;
cursor = context.window().buffer_coord(key.mouse_coord());
cursor = context.window().buffer_coord(key.coord());
context.selections_write_only() =
SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} };
return true;
@ -94,7 +94,7 @@ struct MouseHandler
case Key::Modifiers::MousePos:
if (not m_dragging)
return true;
cursor = context.window().buffer_coord(key.mouse_coord());
cursor = context.window().buffer_coord(key.coord());
context.selections_write_only() =
SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} };
return true;

View File

@ -27,6 +27,8 @@ struct Key
MouseWheelUp = 1 << 6,
MouseEvent = MousePress | MouseRelease | MousePos |
MouseWheelDown | MouseWheelUp,
Resize = 1 << 7,
};
enum NamedKey : Codepoint
{
@ -75,7 +77,7 @@ struct Key
constexpr bool operator!=(Key other) const { return val() != other.val(); }
constexpr bool operator<(Key other) const { return val() < other.val(); }
constexpr CharCoord mouse_coord() const { return {(int)((key & 0xFFFF0000) >> 16), (int)(key & 0x0000FFFF)}; }
constexpr CharCoord coord() const { return {(int)((key & 0xFFFF0000) >> 16), (int)(key & 0x0000FFFF)}; }
Optional<Codepoint> codepoint() const;
};
@ -94,13 +96,15 @@ constexpr Key alt(Codepoint key) { return { Key::Modifiers::Alt, key }; }
constexpr Key ctrl(Codepoint key) { return { Key::Modifiers::Control, key }; }
constexpr Key ctrlalt(Codepoint key) { return { Key::Modifiers::ControlAlt, key }; }
constexpr Codepoint encode_mouse_coord(CharCoord coord) { return (Codepoint)(((int)coord.line << 16) | ((int)coord.column & 0x0000FFFF)); }
constexpr Codepoint encode_coord(CharCoord coord) { return (Codepoint)(((int)coord.line << 16) | ((int)coord.column & 0x0000FFFF)); }
constexpr Key mouse_press(CharCoord pos) { return { Key::Modifiers::MousePress, encode_mouse_coord(pos) }; }
constexpr Key mouse_release(CharCoord pos) { return { Key::Modifiers::MouseRelease, encode_mouse_coord(pos) }; }
constexpr Key mouse_pos(CharCoord pos) { return { Key::Modifiers::MousePos, encode_mouse_coord(pos) }; }
constexpr Key mouse_wheel_down(CharCoord pos) { return { Key::Modifiers::MouseWheelDown, encode_mouse_coord(pos) }; }
constexpr Key mouse_wheel_up(CharCoord pos) { return { Key::Modifiers::MouseWheelUp, encode_mouse_coord(pos) }; }
constexpr Key mouse_press(CharCoord pos) { return { Key::Modifiers::MousePress, encode_coord(pos) }; }
constexpr Key mouse_release(CharCoord pos) { return { Key::Modifiers::MouseRelease, encode_coord(pos) }; }
constexpr Key mouse_pos(CharCoord pos) { return { Key::Modifiers::MousePos, encode_coord(pos) }; }
constexpr Key mouse_wheel_down(CharCoord pos) { return { Key::Modifiers::MouseWheelDown, encode_coord(pos) }; }
constexpr Key mouse_wheel_up(CharCoord pos) { return { Key::Modifiers::MouseWheelUp, encode_coord(pos) }; }
constexpr Key resize(CharCoord dim) { return { Key::Modifiers::Resize, encode_coord(dim) }; }
inline size_t hash_value(const Key& key) { return hash_values(key.modifiers, key.key); }

View File

@ -542,6 +542,7 @@ Key NCursesUI::get_key()
case KEY_HOME: return Key::Home;
case KEY_END: return Key::End;
case KEY_BTAB: return Key::BackTab;
case KEY_RESIZE: return resize(m_dimensions);
}
for (int i = 0; i < 12; ++i)

View File

@ -401,11 +401,8 @@ Key RemoteUI::get_key()
try
{
Key key = read<Key>(m_socket_watcher.fd());
if (key.modifiers == resize_modifier)
{
m_dimensions = { (int)(key.key >> 16), (int)(key.key & 0xFFFF) };
return Key::Invalid;
}
if (key.modifiers == Key::Modifiers::Resize)
m_dimensions = key.coord();
return key;
}
catch (peer_disconnected&)
@ -449,7 +446,7 @@ static int connect_to(StringView session)
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, StringView init_command)
: m_ui(std::move(ui)), m_dimensions(m_ui->dimensions())
: m_ui(std::move(ui))
{
int sock = connect_to(session);
@ -458,10 +455,6 @@ RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&&
msg.write(init_command.data(), (int)init_command.length());
msg.write((char)0);
msg.write(env_vars);
Key key{ resize_modifier, Codepoint(((int)m_dimensions.line << 16) |
(int)m_dimensions.column) };
msg.write(key);
}
m_ui->set_input_callback([this](EventMode){ write_next_key(); });
@ -548,15 +541,6 @@ void RemoteClient::write_next_key()
// do that before checking dimensions as get_key may
// handle a resize event.
msg.write(m_ui->get_key());
CharCoord dimensions = m_ui->dimensions();
if (dimensions != m_dimensions)
{
m_dimensions = dimensions;
Key key{ resize_modifier, Codepoint(((int)dimensions.line << 16) |
(int)dimensions.column) };
msg.write(key);
}
}
void send_command(StringView session, StringView command)

View File

@ -38,7 +38,6 @@ private:
std::unique_ptr<UserInterface> m_ui;
std::unique_ptr<FDWatcher> m_socket_watcher;
CharCoord m_dimensions;
};
void send_command(StringView session, StringView command);