Skip to content

Commit

Permalink
fix an issue where the text area cache is not created immediately aft…
Browse files Browse the repository at this point in the history
…er startup
  • Loading branch information
pit-ray committed Jul 25, 2021
1 parent 80fc2f1 commit 8d6b861
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.clangd
.vscode
.VSCodeCounter
bin
build
cmake_modules
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ If you want to know the details, refer to <a href="https://pit-ray.github.io/win
<br>

## Download
### 4.0.1 (Latest Version)
### 4.1.0 (Latest Version)
Date: 2021/7/6

#### 32bit
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.0.1/setup_win-vind_4.0.1_32bit.exe.zip">setup_win-vind_4.0.1_32bit.exe</a>
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.0.1/win-vind_4.0.1_32bit.zip">win-vind_4.0.1_32bit.zip</a>
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.1.0/setup_win-vind_4.1.0_32bit.exe.zip">setup_win-vind_4.1.0_32bit.exe</a>
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.1.0/win-vind_4.1.0_32bit.zip">win-vind_4.1.0_32bit.zip</a>

#### 64bit
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.0.1/setup_win-vind_4.0.1_64bit.exe.zip">setup_win-vind_4.0.1_64bit.exe</a>
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.0.1/win-vind_4.0.1_64bit.zip">win-vind_4.0.1_64bit.zip</a>
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.1.0/setup_win-vind_4.1.0_64bit.exe.zip">setup_win-vind_4.1.0_64bit.exe</a>
- <a href="https://github.com/pit-ray/win-vind/releases/download/v4.1.0/win-vind_4.1.0_64bit.zip">win-vind_4.1.0_64bit.zip</a>

## Usage
You can refer to the installation and quick tutorial in [Usage - win-vind](https://pit-ray.github.io/win-vind/usage/).
Expand Down
10 changes: 4 additions & 6 deletions build.bat
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
@chcp 65001
@echo Usage: build.bat [-debug/-release] [-msvc/-mingw] [32/64]

@if %1 == -help (
exit
)

@if "%1" == "" (
@set build_type=-debug
) else if "%1" == "-help" (
@ exit
) else (
@set build_type=%1
)
Expand Down Expand Up @@ -42,12 +40,12 @@
cmake -B release_32 -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 16 2019" -A win32 -DBIT_TYPE=32 .
cmake --build release_32 --config Release
xcopy /e /Y ".\\release_32\\coregui\\Release\\*.exe" "release_32"
@rem xcopy /e /Y ".\\release_32\\wxgui\\Release\\*.exe" "release"
@rem xcopy /e /Y ".\\release_32\\wxgui\\Release\\*.exe" "release_32"
) else (
cmake -B release_64 -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 16 2019" -A x64 -DBIT_TYPE=64 .
cmake --build release_64 --config Release
xcopy /e /Y ".\\release_64\\coregui\\Release\\*.exe" "release_64"
@rem xcopy /e /Y ".\\release_64\\wxgui\\Release\\*.exe" "release"
@rem xcopy /e /Y ".\\release_64\\wxgui\\Release\\*.exe" "release_64"
)
) else (
if %3 == 32 (
Expand Down
6 changes: 3 additions & 3 deletions build_resources/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "pit-ray\0"
VALUE "FileDescription", "win-vind (64-bit)\0"
VALUE "FileDescription", "win-vind (32-bit)\0"
VALUE "LegalCopyright", "Copyright (c) 2020 pit-ray\0"
VALUE "InternalName", "win-vind (64-bit)\0"
VALUE "InternalName", "win-vind (32-bit)\0"
VALUE "OriginalFilename", "win-vind.exe\0"
VALUE "ProductName", "win-vind (64bit)\0"
VALUE "ProductName", "win-vind (32bit)\0"
VALUE "ProductVersion", "4.1.0\0"
END
END
Expand Down
7 changes: 6 additions & 1 deletion core/include/bind/mode/change_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _CHANGE_MODE_HPP

#include "bind/binded_func_creator.hpp"
#include "bind/mode/text_area_scanner.hpp"

namespace vind
{
Expand All @@ -26,7 +27,11 @@ namespace vind
static void sprocess(const CharLogger& parent_lgr) ;
} ;

struct ToEdiNormal : public BindedFuncCreator<ToEdiNormal> {
class ToEdiNormal : public BindedFuncCreator<ToEdiNormal> {
private:
static TextAreaScanner scanner_ ;

public:
explicit ToEdiNormal() ;
static void sprocess(bool vclmodeout=true) ;
static void sprocess(NTypeLogger& parent_lgr) ;
Expand Down
16 changes: 8 additions & 8 deletions core/src/bind/mode/change_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,16 @@ namespace vind
sprocess(true) ;
}


// All instances share TextAreaScanner to keep staticity of sprocess.
TextAreaScanner ToEdiNormal::scanner_ ;

//ToEdiNormal
ToEdiNormal::ToEdiNormal()
: BindedFuncCreator("to_edi_normal")
{}
{
AsyncUIACacheBuilder::register_properties(scanner_.get_properties()) ;
}
void ToEdiNormal::sprocess(bool vclmodeout) {
using namespace mode ;
auto mode = get_global_mode() ;
Expand Down Expand Up @@ -149,13 +155,7 @@ namespace vind
throw RUNTIME_EXCEPT("Could not get the cursor position.") ;
}

// All instances share TextAreaScanner to keep staticity of sprocess.
static auto scanner = [] {
TextAreaScanner walker ;
AsyncUIACacheBuilder::register_properties(walker.get_properties()) ;
return walker ;
}() ;
options::focus_nearest_textarea(hwnd, pos, scanner) ;
options::focus_nearest_textarea(hwnd, pos, scanner_) ;
}
}
void ToEdiNormal::sprocess(NTypeLogger& parent_lgr) {
Expand Down
6 changes: 2 additions & 4 deletions core/src/bind/mode/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ namespace vind

for(auto& elem : editables) {
// scan GUI objects only at leaves in tree.
RECT rect ;
if(util::is_failed(elem->get_CachedBoundingRectangle(&rect))) {
Box2D box ;
if(util::is_failed(elem->get_CachedBoundingRectangle(&box.data()))) {
throw RUNTIME_EXCEPT("Could not get the a rectangle of a element.") ;
}

Box2D box(std::move(rect)) ;

auto distance = util::l2_distance_nosq(
point.x(), point.y(),
box.center_x(), box.center_y()) / 100 ;
Expand Down
60 changes: 32 additions & 28 deletions core/src/opt/async_uia_cache_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace
end_ = std::chrono::system_clock::now() ;
}

bool has_stamp() const noexcept {
return end_.time_since_epoch().count() != 0 ;
}

auto elapsed() noexcept {
using namespace std::chrono ;
return duration_cast<milliseconds>(system_clock::now() - begin_) ;
Expand Down Expand Up @@ -83,8 +87,26 @@ namespace
}

bool is_valid() {
if(!time_.has_stamp()) return false ;
return time_.elapsed().count() \
< gparams::get_i("uiacachebuild_validperiod") ;
< gparams::get_i("uiacachebuild_lifetime") ;
}

void create_thread() {
ft_ = std::async(std::launch::async, [this] {
time_.stamp_begin() ;

auto updated = uiauto::update_element(root_, g_cache_request) ;

time_.stamp_end() ;
time_.update_average() ;
return updated ;
}) ;
}

bool check_if_finished() {
using namespace std::chrono ;
return ft_.wait_for(1us) != std::future_status::timeout ;
}

void update() {
Expand All @@ -99,12 +121,9 @@ namespace
}

if(ft_.valid()) {
util::debug::bench_start() ;
using namespace std::chrono ;

// This checks the shared state,
// but it takes several tens of milliseconds to see the shared state.
if(ft_.wait_for(1us) != std::future_status::timeout) {
if(check_if_finished()) {
root_ = ft_.get() ;
}

Expand All @@ -114,40 +133,22 @@ namespace
return ;
}

ft_ = std::async(std::launch::async, [this] {
time_.stamp_begin() ;

auto updated = uiauto::update_element(root_, g_cache_request) ;

time_.stamp_end() ;
time_.update_average() ;
return updated ;
}) ;
create_thread() ;
}

const SmartElement& latest() {
if(!root_) {
update() ;
if(ft_.valid()) {
ft_.wait() ;
root_ = ft_.get() ;
}
return root_ ;
}

if(is_valid()) {
return root_ ;
}

if(ft_.valid()) {
using namespace std::chrono ;
if(ft_.wait_for(1us) != std::future_status::timeout) {
if(check_if_finished()) {
root_ = ft_.get() ;
return root_ ;
}
}
else {
update() ;
create_thread() ;
}

ft_.wait() ;
Expand All @@ -157,6 +158,9 @@ namespace
} ;

std::unordered_map<HWND, WindowUICache> g_caches{} ;
inline auto has_cache(HWND hwnd) noexcept {
return g_caches.find(hwnd) != g_caches.end() ;
}
}


Expand Down Expand Up @@ -218,7 +222,7 @@ namespace vind
return ;
}

if(g_caches.find(hwnd) == g_caches.end()) {
if(!has_cache(hwnd)) {
g_caches[hwnd].initialize(hwnd) ;
}
g_caches[hwnd].update() ;
Expand All @@ -229,7 +233,7 @@ namespace vind
}

SmartElement AsyncUIACacheBuilder::get_root_element(HWND hwnd) {
if(g_caches.find(hwnd) == g_caches.end()) {
if(!has_cache(hwnd)) {
g_caches[hwnd].initialize(hwnd) ;
}
return g_caches[hwnd].latest() ;
Expand Down
2 changes: 1 addition & 1 deletion default_config/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@
"en": "Build UI Automation cache asynchronously",
"ja": "非同期UI Automationキャッシュ生成"
},
"uiacachebuild_validperiod": {
"uiacachebuild_lifetime": {
"value": 1500,
"en": "Valid period of UI Automation cache(ms)",
"ja": "非同期UI Automationキャッシュの有効期間(ms)"
Expand Down
6 changes: 2 additions & 4 deletions scripts/setup_libs.bat
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
@chcp 65001
@echo Usage: setup_libs.bat [-msvc/-mingw] [32/64] [-update (optional)]

@if %1 == -help (
@exit
)

@if not defined NUMBER_OF_PROCESSORS (
@echo Error: This script will not work with powershell.
@exit
Expand All @@ -15,6 +11,8 @@
@echo Error: Please pass your compiler type -mingw or -msvc as the first argument.
@echo.
@exit
) else if "%1" == "-help" (
@exit
)

@echo The number of processors is %NUMBER_OF_PROCESSORS%.
Expand Down

0 comments on commit 8d6b861

Please sign in to comment.