Skip to content

Commit

Permalink
add move_window_* and fix the prefix-number parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
pit-ray committed Jan 3, 2024
1 parent 0821423 commit 23ac333
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.6.0)
project(win-vind VERSION 5.6.0)
set(INTERNAL_VERSION ${PROJECT_VERSION}.0)
set(INTERNAL_VERSION ${PROJECT_VERSION}.1)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
Expand Down
6 changes: 6 additions & 0 deletions res/resources/defaults/small.vindrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ gnnoremap <C-w>k <select_upper_window>
gnnoremap <C-w>j <select_lower_window>
gnnoremap <C-w>s <switch_window>

" @ Window Movement
gnnoremap <C-w><C-h> <move_window_left>
gnnoremap <C-w><C-l> <move_window_right>
gnnoremap <C-w><C-k> <move_window_up>
gnnoremap <C-w><C-j> <move_window_down>

" @ Window Resize
gnnoremap <C-w>= <arrange_windows>
gnnoremap <C-w>r <rotate_windows>
Expand Down
15 changes: 10 additions & 5 deletions src/bind/bindinglist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "window/close_win.hpp"
#include "window/exchange_win.hpp"
#include "window/minmax_win.hpp"
#include "window/move_win.hpp"
#include "window/reload_win.hpp"
#include "window/resize_win.hpp"
#include "window/rotate_win.hpp"
Expand Down Expand Up @@ -135,6 +136,11 @@ namespace vind
MakeDir::create(),
MaximizeCurrentWindow::create(),
MinimizeCurrentWindow::create(),
MoveBckBigWord::create(),
MoveBckEndBigWord::create(),
MoveBckEndWord::create(),
MoveBckWord::create(),
MoveBckWordSimple::create(),
MoveCaretDown::create(),
MoveCaretLeft::create(),
MoveCaretNonBlankWordBackward::create(),
Expand All @@ -147,16 +153,15 @@ namespace vind
MoveCursorLeft::create(),
MoveCursorRight::create(),
MoveCursorUp::create(),
MoveBckBigWord::create(),
MoveBckEndBigWord::create(),
MoveBckEndWord::create(),
MoveBckWord::create(),
MoveBckWordSimple::create(),
MoveEndBigWord::create(),
MoveEndWord::create(),
MoveFwdBigWord::create(),
MoveFwdWord::create(),
MoveFwdWordSimple::create(),
MoveWindowDown::create(),
MoveWindowLeft::create(),
MoveWindowRight::create(),
MoveWindowUp::create(),
Open::create(),
OpenNewTab::create(),
OpenNewWindow::create(),
Expand Down
123 changes: 123 additions & 0 deletions src/bind/window/move_win.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "move_win.hpp"

#include "core/settable.hpp"
#include "util/debug.hpp"
#include "util/screen_metrics.hpp"
#include "util/winwrap.hpp"

#include <algorithm>


namespace vind
{
namespace bind
{
MoveWindowLeft::MoveWindowLeft()
: BindedFuncVoid("move_window_left")
{}

void MoveWindowLeft::sprocess(
std::uint16_t count,
const std::string& UNUSED(args)) {
auto hwnd = util::get_foreground_window() ;
if(!hwnd) {
return ;
}

auto delta = core::SetTable::get_instance().get("window_velocity").get<long>() ;
delta *= static_cast<long>(count) ;

auto rect = util::get_window_rect(hwnd) ;
auto cb_rect = util::get_combined_metrics() ;

auto left = (std::max)(rect.left() - delta, cb_rect.left()) ;
auto top = rect.top() ;
auto width = rect.width() ;
auto height = rect.height() ;

util::move_window(hwnd, left, top, width, height) ;
}

MoveWindowRight::MoveWindowRight()
: BindedFuncVoid("move_window_right")
{}

void MoveWindowRight::sprocess(
std::uint16_t count,
const std::string& UNUSED(args)) {
auto hwnd = util::get_foreground_window() ;
if(!hwnd) {
return ;
}

auto delta = core::SetTable::get_instance().get("window_velocity").get<long>() ;
delta *= static_cast<long>(count) ;

auto rect = util::get_window_rect(hwnd) ;
auto width = rect.width() ;
auto height = rect.height() ;

auto cb_rect = util::get_combined_metrics() ;
auto max_x = (std::max)(cb_rect.right() - width, static_cast<long>(0)) ;

auto left = (std::min)(rect.left() + delta, max_x) ;
auto top = rect.top() ;

util::move_window(hwnd, left, top, width, height) ;
}

MoveWindowUp::MoveWindowUp()
: BindedFuncVoid("move_window_up")
{}

void MoveWindowUp::sprocess(
std::uint16_t count,
const std::string& UNUSED(args)) {
auto hwnd = util::get_foreground_window() ;
if(!hwnd) {
return ;
}

auto delta = core::SetTable::get_instance().get("window_velocity").get<long>() ;
delta *= static_cast<long>(count) ;

auto rect = util::get_window_rect(hwnd) ;
auto cb_rect = util::get_combined_metrics() ;

auto left = rect.left() ;
auto top = (std::max)(rect.top() - delta, cb_rect.top()) ;
auto width = rect.width() ;
auto height = rect.height() ;

util::move_window(hwnd, left, top, width, height) ;
}

MoveWindowDown::MoveWindowDown()
: BindedFuncVoid("move_window_down")
{}

void MoveWindowDown::sprocess(
std::uint16_t count,
const std::string& UNUSED(args)) {
auto hwnd = util::get_foreground_window() ;
if(!hwnd) {
return ;
}

auto delta = core::SetTable::get_instance().get("window_velocity").get<long>() ;
delta *= static_cast<long>(count) ;

auto rect = util::get_window_rect(hwnd) ;
auto width = rect.width() ;
auto height = rect.height() ;

auto cb_rect = util::get_combined_metrics() ;
auto max_y = (std::max)(cb_rect.bottom() - height, static_cast<long>(0)) ;

auto left = rect.left() ;
auto top = (std::min)(rect.top() + delta, max_y) ;

util::move_window(hwnd, left, top, width, height) ;
}
}
}
36 changes: 36 additions & 0 deletions src/bind/window/move_win.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef _MOVEWINDOW_HPP
#define _MOVEWINDOW_HPP

#include "bind/bindedfunc.hpp"

namespace vind
{
namespace bind
{
struct MoveWindowLeft : public BindedFuncVoid<MoveWindowLeft> {
explicit MoveWindowLeft() ;
static void sprocess(
std::uint16_t count, const std::string& args) ;
} ;

struct MoveWindowRight : public BindedFuncVoid<MoveWindowRight> {
explicit MoveWindowRight() ;
static void sprocess(
std::uint16_t count, const std::string& args) ;
} ;

struct MoveWindowUp : public BindedFuncVoid<MoveWindowUp> {
explicit MoveWindowUp() ;
static void sprocess(
std::uint16_t count, const std::string& args) ;
} ;

struct MoveWindowDown : public BindedFuncVoid<MoveWindowDown> {
explicit MoveWindowDown() ;
static void sprocess(
std::uint16_t count, const std::string& args) ;
} ;
}
}

#endif
Empty file removed src/bind/window/movewindow.cpp
Empty file.
4 changes: 0 additions & 4 deletions src/bind/window/movewindow.hpp

This file was deleted.

19 changes: 11 additions & 8 deletions src/core/inputhub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ namespace vind
}

auto solver = get_solver(mode) ;
if(parse_count && !solver->is_matching_any()) {
if(!pimpl->count_.empty() && pimpl->count_.back() == ';') {
pimpl->count_.clear() ; // Erase previous counts.
}

if(parse_count && solver->max_history_size() == 0) {
// parses the sequence of head counts as the string
// and converts them into a number.
auto new_count = extract_numbers(
Expand All @@ -160,13 +156,14 @@ namespace vind
}

if(!pimpl->count_.empty()) {
pimpl->count_ += ';' ; // Final symbol of counts
if(pimpl->count_.back() != ';') {
pimpl->count_ += ';' ; // Final symbol of counts
}
count = util::extract_num<std::uint16_t>(pimpl->count_) ;
}
else {
count = 1 ;
}

fetched_input = input ;
return true ;
}
Expand Down Expand Up @@ -239,7 +236,13 @@ namespace vind
std::uint16_t prefix_count,
Mode mode) {
auto solver = get_solver(mode) ;
auto map_cmd = solver->map_command_from(*input) ;
auto map_cmd = solver->map_command_from(*input, false) ;
if(solver->is_accepted_any() || solver->is_rejected_all()) {
if(!pimpl->count_.empty() && pimpl->count_.back() == ';') {
pimpl->count_.clear() ; // Erase previous counts.
}
solver->reset_state() ;
}
if(map_cmd.empty()) {
return false ;
}
Expand Down
12 changes: 11 additions & 1 deletion src/core/mapsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ namespace vind

bool MapSolver::is_matching_any() const noexcept {
for(const auto& map : pimpl->deployed_) {
if(map.trigger_matcher.history_size() > 0) {
if(map.trigger_matcher.is_matching()) {
return true ;
}
}
Expand Down Expand Up @@ -603,5 +603,15 @@ namespace vind
}
return flag ;
}

std::size_t MapSolver::max_history_size() const noexcept {
std::size_t size = 0 ;
for(const auto& map : pimpl->deployed_) {
if(map.trigger_matcher.history_size() > size) {
size = map.trigger_matcher.history_size() ;
}
}
return size ;
}
}
}
3 changes: 3 additions & 0 deletions src/core/mapsolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ namespace vind
bool is_accepted_any() const noexcept ;
bool is_rejected_any() const noexcept ;
bool is_rejected_all() const noexcept ;

// Return the maximum history size.
std::size_t max_history_size() const noexcept ;
} ;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/version.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef _VERSION_HPP
#define _VERSION_HPP

#define WIN_VIND_VERSION "5.5.2.0"
#define WIN_VIND_VERSION "5.6.0.1"

#endif
6 changes: 6 additions & 0 deletions src/util/winwrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ namespace vind
return rect ;
}

void move_window(HWND hwnd, long left, long top, long width, long height) {
if(!MoveWindow(hwnd, left, top, width, height, TRUE)) {
throw RUNTIME_EXCEPT("Could not move the foreground window.") ;
}
}

template <typename StdString, typename Path>
inline void create_process_core(
StdString&& cmd,
Expand Down
2 changes: 2 additions & 0 deletions src/util/winwrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace vind
Box2D get_window_rect(HWND hwnd) ;
Box2D get_client_rect(HWND hwnd) ;

void move_window(HWND hwnd, long left, long top, long width, long height) ;

inline BOOL b_to_B(bool b) noexcept {
return b ? TRUE : FALSE ;
}
Expand Down

0 comments on commit 23ac333

Please sign in to comment.