Skip to content

Commit

Permalink
[gui] Support for setting the initial position of GGUI window (#6156)
Browse files Browse the repository at this point in the history
Issue: #6107

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Morcki and pre-commit-ci[bot] committed Sep 26, 2022
1 parent c4174e0 commit 85e2aea
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docs/lang/articles/visualization/ggui.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ It is recommended that you familiarize yourself with GGUI through the examples i
`ti.ui.Window(name, res)` creates a window.

```python
window = ti.ui.Window('Window Title', (640, 360))
window = ti.ui.Window('Window Title', res = (640, 360), pos = (150, 150))
```
The argument `res` means resulotion(width and height) of the window, `pos` means the position of the window which origins from the left-top of your main screen.

The following three types of objects can be displayed on a `ti.ui.Window`:

Expand Down
14 changes: 10 additions & 4 deletions python/taichi/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ class Window:
res (tuple[int]): resolution (width, height) of the window, in pixels.
vsync (bool): whether or not vertical sync should be enabled.
show_window (bool): where or not display the window after initialization.
pos (tuple[int]): position (left to right, up to bottom) of the window which origins from the left-top of your main screen, in pixels.
"""
def __init__(self, name, res, vsync=False, show_window=True):
def __init__(self,
name,
res,
vsync=False,
show_window=True,
pos=(100, 100)):
check_ggui_availability()
package_path = str(pathlib.Path(__file__).parent.parent)
ti_arch = default_cfg().arch
is_packed = default_cfg().packed
self.window = _ti_core.PyWindow(get_runtime().prog, name, res, vsync,
show_window, package_path, ti_arch,
is_packed)
self.window = _ti_core.PyWindow(get_runtime().prog, name, res, pos,
vsync, show_window, package_path,
ti_arch, is_packed)

@property
def running(self):
Expand Down
18 changes: 13 additions & 5 deletions taichi/python/export_ggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,22 @@ struct PyWindow {
PyWindow(Program *prog,
std::string name,
py::tuple res,
py::tuple pos,
bool vsync,
bool show_window,
std::string package_path,
Arch ti_arch,
bool is_packed_mode) {
AppConfig config = {name, res[0].cast<int>(), res[1].cast<int>(),
vsync, show_window, package_path,
ti_arch, is_packed_mode};
AppConfig config = {name,
res[0].cast<int>(),
res[1].cast<int>(),
pos[0].cast<int>(),
pos[1].cast<int>(),
vsync,
show_window,
package_path,
ti_arch,
is_packed_mode};
// todo: support other ggui backends
if (!(taichi::arch_is_cpu(ti_arch) || ti_arch == Arch::vulkan ||
ti_arch == Arch::cuda)) {
Expand Down Expand Up @@ -480,8 +488,8 @@ void export_ggui(py::module &m) {
m.attr("GGUI_AVAILABLE") = py::bool_(true);

py::class_<PyWindow>(m, "PyWindow")
.def(py::init<Program *, std::string, py::tuple, bool, bool, std::string,
Arch, bool>())
.def(py::init<Program *, std::string, py::tuple, py::tuple, bool, bool,
std::string, Arch, bool>())
.def("get_canvas", &PyWindow::get_canvas)
.def("show", &PyWindow::show)
.def("get_window_shape", &PyWindow::get_window_shape)
Expand Down
2 changes: 2 additions & 0 deletions taichi/ui/common/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct AppConfig {
std::string name;
int width{0};
int height{0};
int window_pos_x{0};
int window_pos_y{0};
bool vsync{false};
bool show_window{true};
std::string package_path;
Expand Down
3 changes: 2 additions & 1 deletion taichi/ui/common/window_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace taichi::ui {
WindowBase ::WindowBase(AppConfig config) : config_(config) {
if (config_.show_window) {
glfw_window_ = create_glfw_window_(config_.name, config_.width,
config_.height, config_.vsync);
config_.height, config_.window_pos_x,
config_.window_pos_y, config_.vsync);
glfwSetWindowUserPointer(glfw_window_, this);
set_callbacks();
last_record_time_ = glfwGetTime();
Expand Down
10 changes: 9 additions & 1 deletion taichi/ui/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ static void glfw_error_callback(int code, const char *description) {
inline GLFWwindow *create_glfw_window_(const std::string &name,
int screenWidth,
int screenHeight,
int window_pos_x,
int window_pos_y,
bool vsync) {
initGLFW();
GLFWwindow *window;

glfwSetErrorCallback(glfw_error_callback);

glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

window = glfwCreateWindow(screenWidth, screenHeight, name.c_str(), nullptr,
Expand All @@ -82,6 +84,12 @@ inline GLFWwindow *create_glfw_window_(const std::string &name,
printf("GLFW reports no Vulkan support\n");
}

// Reset the window hints to default
glfwDefaultWindowHints();

glfwSetWindowPos(window, window_pos_x, window_pos_y);

glfwShowWindow(window);
// Invalid for Vulkan
/*
if (vsync) {
Expand Down

0 comments on commit 85e2aea

Please sign in to comment.