Skip to content

Commit

Permalink
Events listener works
Browse files Browse the repository at this point in the history
  • Loading branch information
snuk182 authored and snuk182 committed Jan 12, 2018
1 parent 8e563df commit 409366f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
/.project
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ path = "src/lib.rs"
plygui-api = {version = "0.0.1", path = "../plygui-api"}
qt_core = {version = "*", path = "../qt_core"}
qt_widgets = {version = "*", path = "../qt_widgets"}
qt_core_custom_events = {version = "*", path = "../qt_core_custom_events"}
lazy_static = {version = "~1.0"}

[features]
markup = ["plygui-api/markup"]
4 changes: 1 addition & 3 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ impl UiApplication for Application {

impl Drop for Application {
fn drop(&mut self) {
unsafe {

}
QApplication::close_all_windows();
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ use super::*;
use std::{ptr, mem, str};
use std::os::raw::c_void;
use std::slice;
use std::ffi::CString;

use plygui_api::{development, ids, layout, types, callbacks};

lazy_static! {
pub static ref PROPERTY: CString = CString::new("plygui").unwrap();
}

/*#[repr(C)]
pub struct CocoaControlBase {
pub control_base: development::UiControlCommon,
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#[macro_use]
extern crate plygui_api;
#[macro_use]
extern crate lazy_static;

extern crate qt_core;
extern crate qt_widgets;
extern crate qt_core_custom_events;
//extern crate qt_gui;

#[macro_use]
pub mod common;

Expand Down
64 changes: 53 additions & 11 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use super::*;
use super::common::*;

use qt_core::string::String;
use qt_core::cpp_utils::CppBox;
use qt_core::connection::Receiver;
use qt_core::variant::Variant;
use qt_core::event::{Type, Event};
use qt_core_custom_events::custom_event_filter::CustomEventFilter;
use qt_core::cpp_utils::{StaticCast, CppBox};
use qt_core::object::Object;
use qt_widgets::main_window::{MainWindow as QMainWindow};
use qt_widgets::widget::Widget as QWidget;
use qt_widgets::application::{Application as QApplication};

use plygui_api::{development, ids, types, callbacks};
use plygui_api::traits::{UiControl, UiWindow, UiSingleContainer, UiMember, UiContainer};
Expand All @@ -20,6 +23,9 @@ pub struct Window {

child: Option<Box<UiControl>>,
h_resize: Option<callbacks::Resize>,


filter: CppBox<CustomEventFilter>,
}

impl Window {
Expand All @@ -41,8 +47,28 @@ impl Window {
window: QMainWindow::new(),
child: None,
h_resize: None,

filter: CustomEventFilter::new(event_handler),
});
unsafe {
let ptr = window.as_ref() as *const _ as u64;
(window.window.as_mut().static_cast_mut() as &mut Object).set_property(PROPERTY.as_ptr() as *const i8, &Variant::new0(ptr));
}
window.window.set_window_title(&String::from_std_str(title));
window.window.resize(match start_size {
types::WindowStartSize::Exact(w, h) => {
(w as i32, h as i32)
}
types::WindowStartSize::Fullscreen => {
let screen = unsafe { (*QApplication::desktop()).screen_geometry(()) };
(screen.width(), screen.height())
}
});
unsafe {
let filter: *mut Object = window.filter.static_cast_mut() as *mut Object;
let qobject: &mut Object = window.window.as_mut().static_cast_mut();
qobject.install_event_filter(filter);
}
window.window.show();
window
}
Expand Down Expand Up @@ -115,25 +141,25 @@ impl UiContainer for Window {
impl UiMember for Window {
fn set_visibility(&mut self, visibility: types::Visibility) {
self.base.visibility = visibility;
unsafe {
if types::Visibility::Visible == visibility {
//msg_send![self.window, setIsVisible: YES];
} else {
//msg_send![self.window, setIsVisible: NO];
}
if types::Visibility::Visible == visibility {
self.window.slots().set_visible();
} else {
self.window.slots().set_hidden();
}
}
fn visibility(&self) -> types::Visibility {
self.base.visibility
}
fn size(&self) -> (u16, u16) {
unimplemented!()
let size = self.window.size();
(size.width() as u16, size.height() as u16)
}
fn on_resize(&mut self, handler: Option<callbacks::Resize>) {
self.h_resize = handler;

}
unsafe fn native_id(&self) -> usize {
unimplemented!()
self.window.win_id() as usize
}

fn is_control_mut(&mut self) -> Option<&mut UiControl> {
Expand Down Expand Up @@ -165,3 +191,19 @@ unsafe fn is_control_mut(_: &mut development::UiMemberCommon) -> Option<&mut dev
impl_size!(Window);
impl_member_id!(MEMBER_ID_WINDOW);

fn event_handler(object: &mut Object, event: &Event) -> bool {
unsafe {
if event.type_() == Type::Resize {
let ptr = object.property(PROPERTY.as_ptr() as *const i8).to_u_long_long();
if ptr != 0 {
let window: &mut Window = ::std::mem::transmute(ptr);
println!("Resize {:?} = {:?}", event.type_(), window.size());
}
let cls = ::std::ffi::CString::from_raw((&*object.meta_object()).class_name() as *mut i8);
println!("name {:?}", cls);
::std::mem::forget(cls);
}
true
}
}

0 comments on commit 409366f

Please sign in to comment.