Skip to content

Commit

Permalink
fix: list windows 接口
Browse files Browse the repository at this point in the history
fix #206
  • Loading branch information
MistEO committed May 4, 2024
1 parent d89175d commit bcdc1ec
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
6 changes: 6 additions & 0 deletions include/MaaToolkit/Win32/MaaToolkitWin32Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ extern "C"
MAA_TOOLKIT_API MaaSize
MaaToolkitSearchWindow(MaaStringView class_name, MaaStringView window_name);

/**
* @brief List all windows.
* @return MaaSize The number of windows found. To get the corresponding window handle, use
*/
MAA_TOOLKIT_API MaaSize MaaToolkitListWindows();

/**
* @brief Get the window handle by index.
*
Expand Down
7 changes: 7 additions & 0 deletions source/MaaToolkit/API/MaaToolkitWin32Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ MaaSize MaaToolkitSearchWindow(MaaStringView class_name, MaaStringView window_na
return win32_mgr.search_window(class_name, window_name);
}

MaaSize MaaToolkitListWindows()
{
LogInfo;

return win32_mgr.list_windows();
}

MaaWin32Hwnd MaaToolkitGetWindow(MaaSize index)
{
return win32_mgr.found_windows().at(index).hwnd;
Expand Down
1 change: 1 addition & 0 deletions source/MaaToolkit/Win32Window/Win32WindowAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MaaWin32WindowAPI

virtual size_t find_window(std::string_view class_name, std::string_view window_name) = 0;
virtual size_t search_window(std::string_view class_name, std::string_view window_name) = 0;
virtual size_t list_windows() = 0;
virtual std::vector<Window> found_windows() const = 0;

virtual MaaWin32Hwnd get_cursor_window() const = 0;
Expand Down
12 changes: 9 additions & 3 deletions source/MaaToolkit/Win32Window/Win32WindowFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ size_t Win32WindowFinder::find_window(std::string_view class_name, std::string_v
{
windows_.clear();

auto windows = list_windows();
auto windows = _list_windows();
for (auto& w : windows) {
bool same_class = class_name.empty() || w.class_name == class_name;
bool same_window = window_name.empty() || w.window_name == window_name;
Expand All @@ -38,7 +38,7 @@ size_t Win32WindowFinder::search_window(std::string_view class_name, std::string
std::wregex class_regex(to_u16(class_name));
std::wregex window_regex(to_u16(window_name));

auto windows = list_windows();
auto windows = _list_windows();
for (auto& w : windows) {
std::wstring w_class = to_u16(w.class_name);
std::wsmatch class_matches;
Expand All @@ -55,6 +55,12 @@ size_t Win32WindowFinder::search_window(std::string_view class_name, std::string
return windows_.size();
}

size_t Win32WindowFinder::list_windows()
{
windows_ = _list_windows();
return windows_.size();
}

MaaWin32Hwnd Win32WindowFinder::get_cursor_window() const
{
POINT pt {};
Expand All @@ -80,7 +86,7 @@ MaaWin32Hwnd Win32WindowFinder::get_foreground_window() const
return GetForegroundWindow();
}

std::vector<Win32WindowFinder::Window> Win32WindowFinder::list_windows() const
std::vector<Win32WindowFinder::Window> Win32WindowFinder::_list_windows() const
{
std::vector<Window> windows;

Expand Down
3 changes: 2 additions & 1 deletion source/MaaToolkit/Win32Window/Win32WindowFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Win32WindowFinder
virtual size_t find_window(std::string_view class_name, std::string_view window_name) override;
virtual size_t
search_window(std::string_view class_name, std::string_view window_name) override;
virtual size_t list_windows() override;

virtual std::vector<Window> found_windows() const override { return windows_; }

Expand All @@ -29,7 +30,7 @@ class Win32WindowFinder
virtual std::optional<std::string> get_window_name(MaaWin32Hwnd hwnd) const override;

private:
std::vector<Window> list_windows() const;
std::vector<Window> _list_windows() const;

private:
std::vector<Window> windows_;
Expand Down
8 changes: 7 additions & 1 deletion source/MaaToolkit/Win32Window/Win32WindowFinder_NotImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ size_t Win32WindowFinder::search_window(std::string_view class_name, std::string
return 0;
}

size_t Win32WindowFinder::list_windows()
{
LogError << "Not implemented";
return 0;
}

MaaWin32Hwnd Win32WindowFinder::get_cursor_window() const
{
LogError << "Not implemented";
Expand All @@ -42,7 +48,7 @@ MaaWin32Hwnd Win32WindowFinder::get_foreground_window() const
return nullptr;
}

std::vector<Win32WindowFinder::Window> Win32WindowFinder::list_windows() const
std::vector<Win32WindowFinder::Window> Win32WindowFinder::_list_windows() const
{
LogError << "Not implemented";
return {};
Expand Down
4 changes: 4 additions & 0 deletions source/binding/Python/maa/library.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ class _Toolkit:
class_name: MaaStringView, window_name: MaaStringView
) -> MaaSize: ...
@staticmethod
def MaaToolkitListWindows(
class_name: MaaStringView, window_name: MaaStringView
) -> MaaSize: ...
@staticmethod
def MaaToolkitGetWindow(index: MaaSize) -> MaaWin32Hwnd: ...
@staticmethod
def MaaToolkitGetWindowClassName(
Expand Down
15 changes: 15 additions & 0 deletions source/binding/Python/maa/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ def search_window(cls, class_regex: str, window_regex: str) -> List[MaaWin32Hwnd

return windows

@classmethod
def list_windows(cls) -> List[MaaWin32Hwnd]:
cls._set_api_properties()

count = Library.toolkit.MaaToolkitListWindows()

windows = []
for i in range(count):
windows.append(Library.toolkit.MaaToolkitGetWindow(i))

return windows

@classmethod
def get_cursor_window(cls) -> MaaWin32Hwnd:
cls._set_api_properties()
Expand Down Expand Up @@ -246,6 +258,9 @@ def _set_api_properties():
MaaStringView,
]

Library.toolkit.MaaToolkitListWindows.restype = MaaSize
Library.toolkit.MaaToolkitListWindows.argtypes = None

Library.toolkit.MaaToolkitGetWindow.restype = MaaWin32Hwnd
Library.toolkit.MaaToolkitGetWindow.argtypes = [MaaSize]

Expand Down

0 comments on commit bcdc1ec

Please sign in to comment.