2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
21 using Tizen.NUI.BaseComponents;
26 /// The window class is used internally for drawing.<br />
27 /// The window has an orientation and indicator properties.<br />
29 /// <since_tizen> 3 </since_tizen>
30 public partial class Window
32 private WindowFocusChangedEventCallbackType _windowFocusChangedEventCallback;
33 private RootLayerTouchDataCallbackType _rootLayerTouchDataCallback;
34 private WheelEventCallbackType _wheelEventCallback;
35 private EventCallbackDelegateType1 _stageKeyCallbackDelegate;
36 private EventCallbackDelegateType0 _stageEventProcessingFinishedEventCallbackDelegate;
37 private EventHandler _stageContextLostEventHandler;
38 private EventCallbackDelegateType0 _stageContextLostEventCallbackDelegate;
39 private EventHandler _stageContextRegainedEventHandler;
40 private EventCallbackDelegateType0 _stageContextRegainedEventCallbackDelegate;
41 private EventHandler _stageSceneCreatedEventHandler;
42 private EventCallbackDelegateType0 _stageSceneCreatedEventCallbackDelegate;
43 private WindowResizedEventCallbackType _windowResizedEventCallback;
44 private WindowFocusChangedEventCallbackType _windowFocusChangedEventCallback2;
45 private TransitionEffectEventCallbackType transitionEffectEventCallback;
46 private WindowTransitionEffectSignal transitionEffectSignal;
48 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
49 private delegate void WindowFocusChangedEventCallbackType(bool focusGained);
50 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
51 private delegate bool RootLayerTouchDataCallbackType(IntPtr view, IntPtr touchData);
52 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
53 private delegate bool WheelEventCallbackType(IntPtr view, IntPtr wheelEvent);
54 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
55 private delegate void WindowResizedEventCallbackType(IntPtr windowSize);
56 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
57 private delegate void WindowFocusChangedEventCallbackType2(bool focusGained);
58 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
59 private delegate void TransitionEffectEventCallbackType(IntPtr window, int state, int type);
62 /// FocusChanged event.
64 /// <since_tizen> 3 </since_tizen>
65 public event EventHandler<FocusChangedEventArgs> FocusChanged
69 if (_windowFocusChangedEventHandler == null)
71 _windowFocusChangedEventCallback = OnWindowFocusedChanged;
72 WindowFocusChangedSignal().Connect(_windowFocusChangedEventCallback);
75 _windowFocusChangedEventHandler += value;
79 _windowFocusChangedEventHandler -= value;
81 if (_windowFocusChangedEventHandler == null && WindowFocusChangedSignal().Empty() == false && _windowFocusChangedEventCallback != null)
83 WindowFocusChangedSignal().Disconnect(_windowFocusChangedEventCallback);
89 /// This event is emitted when the screen is touched and when the touch ends.<br />
90 /// If there are multiple touch points, then this will be emitted when the first touch occurs and
91 /// then when the last finger is lifted.<br />
92 /// An interrupted event will also be emitted (if it occurs).<br />
94 /// <since_tizen> 3 </since_tizen>
95 public event EventHandler<TouchEventArgs> TouchEvent
99 if (_rootLayerTouchDataEventHandler == null)
101 _rootLayerTouchDataCallback = OnWindowTouch;
102 this.TouchDataSignal().Connect(_rootLayerTouchDataCallback);
104 _rootLayerTouchDataEventHandler += value;
108 _rootLayerTouchDataEventHandler -= value;
109 if (_rootLayerTouchDataEventHandler == null && TouchSignal().Empty() == false)
111 this.TouchDataSignal().Disconnect(_rootLayerTouchDataCallback);
117 /// This event is emitted when the wheel event is received.
119 /// <since_tizen> 3 </since_tizen>
120 public event EventHandler<WheelEventArgs> WheelEvent
124 if (_stageWheelHandler == null)
126 _wheelEventCallback = OnStageWheel;
127 this.StageWheelEventSignal().Connect(_wheelEventCallback);
129 _stageWheelHandler += value;
133 _stageWheelHandler -= value;
134 if (_stageWheelHandler == null && StageWheelEventSignal().Empty() == false)
136 this.StageWheelEventSignal().Disconnect(_wheelEventCallback);
142 /// This event is emitted when the key event is received.
144 /// <since_tizen> 3 </since_tizen>
145 public event EventHandler<KeyEventArgs> KeyEvent
149 if (_stageKeyHandler == null)
151 _stageKeyCallbackDelegate = OnStageKey;
152 KeyEventSignal().Connect(_stageKeyCallbackDelegate);
154 _stageKeyHandler += value;
158 _stageKeyHandler -= value;
159 if (_stageKeyHandler == null && KeyEventSignal().Empty() == false)
161 KeyEventSignal().Disconnect(_stageKeyCallbackDelegate);
167 /// This event is emitted when the window resized.
169 /// <since_tizen> 3 </since_tizen>
170 public event EventHandler<ResizedEventArgs> Resized
174 if (_windowResizedEventHandler == null)
176 _windowResizedEventCallback = OnResized;
177 ResizedSignal().Connect(_windowResizedEventCallback);
180 _windowResizedEventHandler += value;
184 _windowResizedEventHandler -= value;
186 if (_windowResizedEventHandler == null && ResizedSignal().Empty() == false && _windowResizedEventCallback != null)
188 ResizedSignal().Disconnect(_windowResizedEventCallback);
194 /// Please do not use! this will be deprecated. Please use 'FocusChanged' event instead.
196 /// <since_tizen> 3 </since_tizen>
197 /// Please do not use! this will be deprecated!
198 /// Instead please use FocusChanged.
199 [Obsolete("Please do not use! This will be deprecated! Please use FocusChanged instead! " +
201 "Window.Instance.FocusChanged = OnFocusChanged; " +
202 "private void OnFocusChanged(object source, Window.FocusChangedEventArgs args) {...}")]
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 public event EventHandler<FocusChangedEventArgs> WindowFocusChanged
208 if (_windowFocusChangedEventHandler2 == null)
210 _windowFocusChangedEventCallback2 = OnWindowFocusedChanged2;
211 WindowFocusChangedSignal().Connect(_windowFocusChangedEventCallback2);
214 _windowFocusChangedEventHandler2 += value;
218 _windowFocusChangedEventHandler2 -= value;
220 if (_windowFocusChangedEventHandler2 == null && WindowFocusChangedSignal().Empty() == false && _windowFocusChangedEventCallback2 != null)
222 WindowFocusChangedSignal().Disconnect(_windowFocusChangedEventCallback2);
230 [EditorBrowsable(EditorBrowsableState.Never)]
231 public event EventHandler<TransitionEffectArgs> TransitionEffect
235 if (transitionEffectHandler == null)
237 transitionEffectEventCallback = OnTransitionEffect;
238 TransitionEffectEventSignal().Connect(transitionEffectEventCallback);
240 transitionEffectHandler += value;
244 transitionEffectHandler -= value;
245 if (transitionEffectHandler == null && TransitionEffectEventSignal().Empty() == false)
247 TransitionEffectEventSignal().Disconnect(transitionEffectEventCallback);
253 /// ViewAdded will be triggered when the view added on Window
255 /// <since_tizen> 6 </since_tizen>
256 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
257 [EditorBrowsable(EditorBrowsableState.Never)]
258 public event EventHandler ViewAdded;
259 private event EventHandler<FocusChangedEventArgs> _windowFocusChangedEventHandler;
260 private event EventHandler<TouchEventArgs> _rootLayerTouchDataEventHandler;
261 private event EventHandler<WheelEventArgs> _stageWheelHandler;
262 private event EventHandler<KeyEventArgs> _stageKeyHandler;
263 private event EventHandler _stageEventProcessingFinishedEventHandler;
264 private event EventHandler<ResizedEventArgs> _windowResizedEventHandler;
265 private event EventHandler<FocusChangedEventArgs> _windowFocusChangedEventHandler2;
266 private event EventHandler<TransitionEffectArgs> transitionEffectHandler;
268 internal void SendViewAdded(View view)
270 ViewAdded?.Invoke(view, EventArgs.Empty);
273 internal event EventHandler EventProcessingFinished
277 if (_stageEventProcessingFinishedEventHandler == null)
279 _stageEventProcessingFinishedEventCallbackDelegate = OnEventProcessingFinished;
280 EventProcessingFinishedSignal().Connect(_stageEventProcessingFinishedEventCallbackDelegate);
282 _stageEventProcessingFinishedEventHandler += value;
287 _stageEventProcessingFinishedEventHandler -= value;
288 if (_stageEventProcessingFinishedEventHandler == null && EventProcessingFinishedSignal().Empty() == false)
290 EventProcessingFinishedSignal().Disconnect(_stageEventProcessingFinishedEventCallbackDelegate);
295 internal event EventHandler ContextLost
299 if (_stageContextLostEventHandler == null)
301 _stageContextLostEventCallbackDelegate = OnContextLost;
302 ContextLostSignal().Connect(_stageContextLostEventCallbackDelegate);
304 _stageContextLostEventHandler += value;
308 _stageContextLostEventHandler -= value;
309 if (_stageContextLostEventHandler == null && ContextLostSignal().Empty() == false)
311 ContextLostSignal().Disconnect(_stageContextLostEventCallbackDelegate);
316 internal event EventHandler ContextRegained
320 if (_stageContextRegainedEventHandler == null)
322 _stageContextRegainedEventCallbackDelegate = OnContextRegained;
323 ContextRegainedSignal().Connect(_stageContextRegainedEventCallbackDelegate);
325 _stageContextRegainedEventHandler += value;
329 _stageContextRegainedEventHandler -= value;
330 if (_stageContextRegainedEventHandler == null && ContextRegainedSignal().Empty() == false)
332 this.ContextRegainedSignal().Disconnect(_stageContextRegainedEventCallbackDelegate);
337 internal event EventHandler SceneCreated
341 if (_stageSceneCreatedEventHandler == null)
343 _stageSceneCreatedEventCallbackDelegate = OnSceneCreated;
344 SceneCreatedSignal().Connect(_stageSceneCreatedEventCallbackDelegate);
346 _stageSceneCreatedEventHandler += value;
350 _stageSceneCreatedEventHandler -= value;
351 if (_stageSceneCreatedEventHandler == null && SceneCreatedSignal().Empty() == false)
353 SceneCreatedSignal().Disconnect(_stageSceneCreatedEventCallbackDelegate);
358 internal WindowFocusSignalType WindowFocusChangedSignal()
360 WindowFocusSignalType ret = new WindowFocusSignalType(Interop.Window.FocusChangedSignal(swigCPtr), false);
361 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
365 internal WindowFocusSignalType FocusChangedSignal()
367 WindowFocusSignalType ret = new WindowFocusSignalType(Interop.Window.FocusChangedSignal(swigCPtr), false);
368 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
372 internal KeyEventSignal KeyEventSignal()
374 KeyEventSignal ret = new KeyEventSignal(Interop.Window.KeyEventSignal(swigCPtr), false);
375 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
379 internal VoidSignal EventProcessingFinishedSignal()
381 VoidSignal ret = new VoidSignal(Interop.StageSignal.Stage_EventProcessingFinishedSignal(stageCPtr), false);
382 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
386 internal TouchSignal TouchSignal()
388 TouchSignal ret = new TouchSignal(Interop.Window.TouchSignal(swigCPtr), false);
389 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
393 internal TouchDataSignal TouchDataSignal()
395 TouchDataSignal ret = new TouchDataSignal(Interop.ActorSignal.Actor_TouchSignal(Layer.getCPtr(GetRootLayer())), false);
396 if (NDalicPINVOKE.SWIGPendingException.Pending)
397 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401 internal VoidSignal ContextLostSignal()
403 VoidSignal ret = new VoidSignal(Interop.StageSignal.Stage_ContextLostSignal(stageCPtr), false);
404 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408 internal VoidSignal ContextRegainedSignal()
410 VoidSignal ret = new VoidSignal(Interop.StageSignal.Stage_ContextRegainedSignal(stageCPtr), false);
411 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415 internal VoidSignal SceneCreatedSignal()
417 VoidSignal ret = new VoidSignal(Interop.StageSignal.Stage_SceneCreatedSignal(stageCPtr), false);
418 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422 internal ResizedSignal ResizedSignal()
424 ResizedSignal ret = new ResizedSignal(Interop.Window.Window_ResizedSignal(swigCPtr), false);
425 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429 internal System.IntPtr GetNativeWindowHandler()
431 System.IntPtr ret = Interop.Window.GetNativeWindowHandler(HandleRef.ToIntPtr(this.swigCPtr));
432 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
437 /// Disconnect all native signals
439 /// <since_tizen> 5 </since_tizen>
440 internal void DisconnectNativeSignals()
442 if (_windowFocusChangedEventCallback != null)
444 WindowFocusChangedSignal().Disconnect(_windowFocusChangedEventCallback);
447 if (_rootLayerTouchDataCallback != null)
449 TouchDataSignal().Disconnect(_rootLayerTouchDataCallback);
452 if (_wheelEventCallback != null)
454 StageWheelEventSignal().Disconnect(_wheelEventCallback);
457 if (_stageKeyCallbackDelegate != null)
459 KeyEventSignal().Disconnect(_stageKeyCallbackDelegate);
462 if (_stageEventProcessingFinishedEventCallbackDelegate != null)
464 EventProcessingFinishedSignal().Disconnect(_stageEventProcessingFinishedEventCallbackDelegate);
467 if (_stageContextLostEventCallbackDelegate != null)
469 ContextLostSignal().Disconnect(_stageContextLostEventCallbackDelegate);
472 if (_stageContextRegainedEventCallbackDelegate != null)
474 ContextRegainedSignal().Disconnect(_stageContextRegainedEventCallbackDelegate);
477 if (_stageSceneCreatedEventCallbackDelegate != null)
479 SceneCreatedSignal().Disconnect(_stageSceneCreatedEventCallbackDelegate);
482 if (_windowResizedEventCallback != null)
484 ResizedSignal().Disconnect(_windowResizedEventCallback);
487 if (_windowFocusChangedEventCallback2 != null)
489 WindowFocusChangedSignal().Disconnect(_windowFocusChangedEventCallback2);
492 if (transitionEffectSignal != null)
494 TransitionEffectEventSignal().Disconnect(transitionEffectEventCallback);
498 private StageWheelSignal WheelEventSignal()
500 StageWheelSignal ret = new StageWheelSignal(Interop.StageSignal.Stage_WheelEventSignal(stageCPtr), false);
501 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
505 private WheelSignal StageWheelEventSignal()
507 WheelSignal ret = new WheelSignal(Interop.ActorSignal.Actor_WheelEventSignal(Layer.getCPtr(this.GetRootLayer())), false);
508 if (NDalicPINVOKE.SWIGPendingException.Pending)
509 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
513 private WindowTransitionEffectSignal TransitionEffectEventSignal()
515 //Tizen.Log.Fatal("NUITEST", "TransitionEffectEventSignal()!");
516 if (transitionEffectSignal == null)
518 transitionEffectSignal = new WindowTransitionEffectSignal(this);
519 if (NDalicPINVOKE.SWIGPendingException.Pending)
520 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
521 //Tizen.Log.Fatal("NUITEST", $"transitionEffectSignal is null, new here!");
523 return transitionEffectSignal;
526 private void OnWindowFocusedChanged(bool focusGained)
528 FocusChangedEventArgs e = new FocusChangedEventArgs();
530 e.FocusGained = focusGained;
532 if (_windowFocusChangedEventHandler != null)
534 _windowFocusChangedEventHandler(this, e);
538 private bool OnWindowTouch(IntPtr view, IntPtr touchData)
540 if (touchData == global::System.IntPtr.Zero)
542 NUILog.Error("touchData should not be null!");
546 TouchEventArgs e = new TouchEventArgs();
548 e.Touch = Tizen.NUI.Touch.GetTouchFromPtr(touchData);
550 if (_rootLayerTouchDataEventHandler != null)
552 _rootLayerTouchDataEventHandler(this, e);
557 private bool OnStageWheel(IntPtr rootLayer, IntPtr wheelEvent)
559 if (wheelEvent == global::System.IntPtr.Zero)
561 NUILog.Error("wheelEvent should not be null!");
565 WheelEventArgs e = new WheelEventArgs();
567 e.Wheel = Tizen.NUI.Wheel.GetWheelFromPtr(wheelEvent);
569 if (_stageWheelHandler != null)
571 _stageWheelHandler(this, e);
576 // Callback for Stage KeyEventsignal
577 private void OnStageKey(IntPtr data)
579 KeyEventArgs e = new KeyEventArgs();
580 e.Key = Tizen.NUI.Key.GetKeyFromPtr(data);
583 if (_stageKeyHandler != null)
585 //here we send all data to user event handlers
586 _stageKeyHandler(this, e);
590 // Callback for Stage EventProcessingFinishedSignal
591 private void OnEventProcessingFinished()
593 if (_stageEventProcessingFinishedEventHandler != null)
595 _stageEventProcessingFinishedEventHandler(this, null);
599 // Callback for Stage ContextLostSignal
600 private void OnContextLost()
602 if (_stageContextLostEventHandler != null)
604 _stageContextLostEventHandler(this, null);
608 // Callback for Stage ContextRegainedSignal
609 private void OnContextRegained()
611 if (_stageContextRegainedEventHandler != null)
613 _stageContextRegainedEventHandler(this, null);
617 // Callback for Stage SceneCreatedSignal
618 private void OnSceneCreated()
620 if (_stageSceneCreatedEventHandler != null)
622 _stageSceneCreatedEventHandler(this, null);
626 private void OnResized(IntPtr windowSize)
628 ResizedEventArgs e = new ResizedEventArgs();
629 var val = new Uint16Pair(windowSize, false);
630 e.WindowSize = new Size2D(val.GetWidth(), val.GetHeight());
633 if (_windowResizedEventHandler != null)
635 _windowResizedEventHandler(this, e);
639 private void OnWindowFocusedChanged2(bool focusGained)
641 FocusChangedEventArgs e = new FocusChangedEventArgs();
643 e.FocusGained = focusGained;
645 if (_windowFocusChangedEventHandler2 != null)
647 _windowFocusChangedEventHandler2(this, e);
651 private void OnTransitionEffect(IntPtr window, int state, int type)
653 //Tizen.Log.Fatal("NUITEST", $"OnTransitionEffect() called");
654 if (window == global::System.IntPtr.Zero)
656 //Tizen.Log.Error("NUI", $"OnTransitionEffect() IntPtr window is null!");
660 TransitionEffectArgs e = new TransitionEffectArgs();
662 e.State = (EffectStates)state;
663 //Tizen.Log.Error("NUITEST", $"e.State={e.State}");
665 e.Type = (EffectTypes)type;
666 //Tizen.Log.Error("NUITEST", $"e.Type={e.Type}");
668 if (transitionEffectHandler != null)
670 //Tizen.Log.Fatal("NUITEST", $"Execute transitionEffectHandler(this, e)!!!");
671 transitionEffectHandler(this, e);
677 /// The focus changed event argument.
679 /// <since_tizen> 3 </since_tizen>
680 public class FocusChangedEventArgs : EventArgs
683 /// FocusGained flag.
685 /// <since_tizen> 3 </since_tizen>
686 public bool FocusGained
694 /// The touch event argument.
696 /// <since_tizen> 3 </since_tizen>
697 public class TouchEventArgs : EventArgs
699 private Touch _touch;
704 /// <since_tizen> 3 </since_tizen>
719 /// Wheel event arguments.
721 /// <since_tizen> 3 </since_tizen>
722 public class WheelEventArgs : EventArgs
724 private Wheel _wheel;
729 /// <since_tizen> 3 </since_tizen>
744 /// Key event arguments.
746 /// <since_tizen> 3 </since_tizen>
747 public class KeyEventArgs : EventArgs
754 /// <since_tizen> 3 </since_tizen>
769 /// Feeds a key event into the window.
770 /// This resized event arguments.
772 /// <since_tizen> 3 </since_tizen>
773 public class ResizedEventArgs : EventArgs
778 /// This window size.
780 /// <since_tizen> 4 </since_tizen>
781 public Size2D WindowSize
795 /// Please do not use! this will be deprecated
797 /// <since_tizen> 3 </since_tizen>
798 [Obsolete("Please do not use! This will be deprecated! Please use FocusChangedEventArgs instead! " +
800 "Window.Instance.FocusChanged = OnFocusChanged; " +
801 "private void OnFocusChanged(object source, Window.FocusChangedEventArgs args) {...}")]
802 [EditorBrowsable(EditorBrowsableState.Never)]
803 public class WindowFocusChangedEventArgs : EventArgs
806 /// Please do not use! this will be deprecated
808 /// <since_tizen> 3 </since_tizen>
809 public bool FocusGained
817 /// Contains and encapsulates Native Window handle.
819 /// <since_tizen> 4 </since_tizen>
820 public class SafeNativeWindowHandle : SafeHandle
823 /// Contructor, Native window handle is set to handle.
825 /// <since_tizen> 4 </since_tizen>
826 public SafeNativeWindowHandle() : base(IntPtr.Zero, false)
828 SetHandle(Tizen.NUI.Window.Instance.GetNativeWindowHandler());
831 /// Null check if the handle is valid or not.
833 /// <since_tizen> 4 </since_tizen>
834 public override bool IsInvalid
838 return this.handle == IntPtr.Zero;
842 /// Release handle itself.
844 /// <returns>true when released successfully.</returns>
845 /// <since_tizen> 4 </since_tizen>
846 protected override bool ReleaseHandle()
853 /// TransitionEffectArgs
855 [EditorBrowsable(EditorBrowsableState.Never)]
856 public class TransitionEffectArgs : EventArgs
858 private EffectStates state;
859 private EffectTypes type;
864 [EditorBrowsable(EditorBrowsableState.Never)]
865 public EffectStates State
879 [EditorBrowsable(EditorBrowsableState.Never)]
880 public EffectTypes Type