
    f*                       d Z ddlmZmZmZ ddlZddlZddlmZm	Z	 ddl
mZ  e        ddlmZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-m.Z. ddl/Z/ e/j`                  d      Z1ddl2m3Z3  e3       Z4ejj                  jl                  ejj                  jn                  z  ejj                  jp                  z  Z9 G d de%      Z: G d de%      Z; G d de%      Z< G d d      Z= G d d      Z> G d de+      Z? G d  dej                  e?e e      ZA G d! d"      ZB G d# d$eB      ZC G d% d&eB      ZD G d' d(ej                        ZFy))z GTK keyboard widget     )divisionprint_functionunicode_literalsN)sinpi)require_gi_versions)GLibGdkGtk)
TouchInputInputSequence)	EventType)LayoutPopupLayoutBuilderAlternativesLayoutBuilder)Key)LOD)TouchHandles)
LayoutView)Rectescape_markup)Timer	FadeTimer)HandleHandleFunction)WindowManipulatorcanvas_to_root_window_rectcanvas_to_root_window_pointget_monitor_dimensionsKeyboardWidget)Configc                   (    e Zd ZdZdZd ZddZd Zy)AutoReleaseTimerz|
    Releases latched and locked modifiers after a period of inactivity.
    Inactivity here means no keys are pressed.
    Nc                     || _         y N	_keyboardselfkeyboards     8/usr/lib/python3/dist-packages/Onboard/KeyboardWidget.py__init__zAutoReleaseTimer.__init__I   	    !    c                     | j                          t        j                  j                  }|dk(  r-t        j                  j                  }|r|rt        ||      }n|}|rt        j                  | |       y y NF)stopconfigr*   sticky_key_release_delay sticky_key_release_on_hide_delayminr   start)r)   visibility_changedelay
hide_delays       r+   r6   zAutoReleaseTimer.startL   s^    		88%IIJz2E&EKKe$ r.   c                    t        t        j                  j                        }|rt        j                  j                  d       | j                  j                          | j                  j                  |       d| j                  _	        | j                  j                          | j                  j                          y)Nr   F)boolr2   r*   r3   word_suggestionsset_pause_learningr'   release_latched_sticky_keysrelease_locked_sticky_keysactive_layer_indexinvalidate_ui_no_resizecommit_ui_updates)r)   release_all_keyss     r+   on_timerzAutoReleaseTimer.on_timerY   s       H HI##66q9224112BC,-)..0((*r.   r%   )__name__
__module____qualname____doc__r'   r,   r6   rD    r.   r+   r#   r#   B   s     I"%r.   r#   c                   6    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
y)	InactivityTimerz
    Waits for the inactivity delay and transitions between
    active and inactive state.
    Inactivity here means, the pointer has left the keyboard window.
    NFc                     || _         y r%   r&   r(   s     r+   r,   zInactivityTimer.__init__q   r-   r.   c                    | j                   j                         }|sy|j                         }|xrU |j                         xrC t        j                         xr- t        j                  j                  xr t        j                   S r0   )	r'   get_kbd_window
get_screenis_compositedr2    is_inactive_transparency_enabledwindowenable_inactive_transparencyxid_mode)r)   rR   screens      r+   
is_enabledzInactivityTimer.is_enabledt   st    ..0""$ #6//1 #668#}}99# ??"	#r.   c                     | j                   S r%   )_activer)   s    r+   	is_activezInactivityTimer.is_active~   s    ||r.   c                 ,   || _         |rLt        j                  |        | j                  j	                  d      r| j                  j                          y y t        j                  s/t        j                  | t        j                  j                         y y NT)rX   r   r1   r'   transition_active_tocommit_transitionr2   rT   r6   rR   inactive_transparency_delay)r)   actives     r+   begin_transitionz InactivityTimer.begin_transition   se    JJt~~2248002 9 ??D&--"K"KL #r.   c                 n    | j                   j                  d       | j                   j                          yr0   )r'   r]   r^   rY   s    r+   rD   zInactivityTimer.on_timer   s'    ++E2((*r.   )rE   rF   rG   rH   r'   rX   r,   rV   rZ   ra   rD   rI   r.   r+   rK   rK   h   s-    
 IG"#Mr.   rK   c                   "    e Zd ZdZd Zd Zd Zy)HideInputLineTimerzG
    Temporarily hides the input line when the pointer touches it.
    c                     || _         y r%   r&   r(   s     r+   r,   zHideInputLineTimer.__init__   r-   r.   c                 8   |j                   }| j                  j                         D ]q  }|j                         }|j	                  |      r#| j                         r5| j                  d       G| j                          | j                  j                  d       s y)z(
        Handle pointer motion.
        333333?FN)	pointr'   get_text_displaysget_canvas_border_rectis_point_within
is_runningr6   r1   hide_input_line)r)   sequencerh   keyrects        r+   handle_motionz HideInputLineTimer.handle_motion   sw      >>335 	6C--/D##E*(JJsO		..u5	6r.   c                 :    | j                   j                  d       y)z! Hide the input line after delay TF)r'   rm   rY   s    r+   rD   zHideInputLineTimer.on_timer   s    &&t,r.   N)rE   rF   rG   rH   r,   rq   rD   rI   r.   r+   rd   rd      s    "6"r.   rd   c                   4    e Zd ZdZdZdZdZdZdZdZ	d Z
d Zy)TransitionVariablez/ A variable taking part in opacity transitions         Fc                     | j                   | _        || _        t        j                         | _        || _        d| _        y)z Begin transition FN)valuestart_valuetarget_valuetime
start_timedurationdone)r)   targetr|   s      r+   start_transitionz#TransitionVariable.start_transition   s1    ::"))+ 	r.   c                 R   | j                   | j                  z
  }|rG| j                  r;t        j                         | j                  z
  }t        d|| j                  z        }nd}t        |t        z  t        dz  z
        dz   dz  }| j                  ||z  z   | _        |dk\  | _	        y)zU
        Update self.value based on the elapsed time since start_transition.
              ?       @N)
ry   rx   r|   rz   r{   r5   r   r   rw   r}   )r)   rangeelapsedlin_progresssin_progresss        r+   updatezTransitionVariable.update   s     !!D$4$44T]]yy{T__4GsGdmm$;<LLL2-S89C?3F%%u(<<
 C'	r.   N)rE   rF   rG   rH   rw   rx   ry   r{   r|   r}   r   r   rI   r.   r+   rt   rt      s-    9EKLJHD(r.   rt   c                   (    e Zd ZdZd Zd Zd Zd Zy)TransitionStatez= Set of all state variables involved in opacity transitions. c                     t               | _        t               | _        t               | _        t               | _        | j                  | j                  | j                  | j                  g| _        d| _        y r0   )rt   visibler`   xy_varstarget_visibilityrY   s    r+   r,   zTransitionState.__init__   sR    )+)+)+)+llDKK@
!&r.   c                 F    | j                   D ]  }|j                           y r%   )r   r   )r)   vars     r+   r   zTransitionState.update   s    :: 	CJJL	r.   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr%   )r}   ).0r   s     r+   	<genexpr>z*TransitionState.is_done.<locals>.<genexpr>   s     23882   )allr   rY   s    r+   is_donezTransitionState.is_done       2tzz222r.   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr%   )r|   )r   r   s     r+   r   z3TransitionState.get_max_duration.<locals>.<genexpr>   s     2!1::2r   )maxr   rY   s    r+   get_max_durationz TransitionState.get_max_duration   r   r.   N)rE   rF   rG   rH   r,   r   r   r   rI   r.   r+   r   r      s    G'33r.   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	WindowManipulatorAspectRatioz6 Adds support for handles with function ASPECT_RATIO. c                 l    t        j                  |        t        j                  j                  | _        y r%   )r   r,   r2   rR   docking_aspect_change_range_docking_aspect_change_rangerY   s    r+   r,   z%WindowManipulatorAspectRatio.__init__   s%    ""4(MM55 	)r.   c                     t         j                  j                  }| j                  |k7  r<|| _        | j                  j                          | j                  j                          yy)z GSettings key changed N)r2   rR   r   r   r*   invalidate_uirB   )r)   rw   s     r+   "update_docking_aspect_change_rangez?WindowManipulatorAspectRatio.update_docking_aspect_change_range   sL    99,,505D-MM'')MM++- 6r.   c                     | j                   S r%   )r   rY   s    r+   get_docking_aspect_change_rangez<WindowManipulatorAspectRatio.get_docking_aspect_change_range   s    000r.   c                 B    | j                   t        j                  _        y r%   )r   r2   rR   r   rY   s    r+   on_drag_donez)WindowManipulatorAspectRatio.on_drag_done   s    -- 	1r.   c                 .    | j                         | _        y r%   )get_keyboard_frame_rect_drag_start_keyboard_frame_rectrY   s    r+   on_handle_aspect_ratio_pressedz;WindowManipulatorAspectRatio.on_handle_aspect_ratio_pressed   s    /3/K/K/M,r.   c                    | j                   }| j                         }|j                  |j                  z  }| j                   j                  }||dz  z   }|| j                  j                  dz  k\  rd}n||j                  |z  z  }d}	||z  }
|
|	k  r|	|z  }| j
                  d   |f| _        | j                          | j                          | j                  | j                         | j                          y )N   gffffff?g      Y@g      ?r   )r   get_base_aspect_rectwhcanvas_rectr   update_layoutupdate_touch_handles_positionsinvalidate_for_resize_lodredraw)r)   dxdykeyboard_frame_rectbase_aspect_rectbase_aspectstart_frame_widthnew_frame_widthnew_aspect_change
min_aspect
new_aspects              r+   on_handle_aspect_ratio_motionz:WindowManipulatorAspectRatio.on_handle_aspect_ratio_motion  s   "BB446&((+;+=+== @@BB+b1f4 d..00J?? %  #6#8#8;#FG  
 #44

" *[ 8 ..q13DE 	) 	++-""499-r.   N)
rE   rF   rG   rH   r,   r   r   r   r   r   rI   r.   r+   r   r      s$    @6
.1.Nr.   r   c                   d   e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd ZdadZd Zd Zd Zd Zd Zd Z	 	 dbdZdadZd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'e(d&        Z)d' Z*d( Z+d) Z,d* Z-d+ Z.d, Z/d- Z0d. Z1d/ Z2d0 Z3dad1Z4d2 Z5d3 Z6d4 Z7d5 Z8d6 Z9d7 Z:d8 Z;d9 Z<d: Z=d; Z>d< Z?d= Z@d> ZAd? ZBd@ ZCdA ZDdB ZEdC ZFdcdDZGdE ZHdF ZIdG ZJdH ZKdI ZLdJ ZMdK ZNdL ZOdM ZPdN ZQdO ZRdP ZSdddQZTdR ZUdS ZVdT ZWdU ZXdV ZYdW ZZdX Z[dY Z\dZ Z]d[ Z^d\ Z_d] Z`dad^Zad_ Zbdad`Zcy)er    g      ?rg   c                    t         j                  j                  |        t        j                  |        t	        j                  | |       t        j                  |        | j                  d       t               | _        d| _	        d| _
        d | _        t               | _        d| _        d| _        d| _        d | _        t               | _        t'        |      | _        d | _        d | _        d | _        d | _        t3        |       | _        t7               | _        t               | _        t=               | _        d| _         d | _!        tE        |      | _#        t               | _$        tK               | _&        d| jL                  jN                  _(        d| jL                  jR                  _(        d| jL                  jT                  _(        d| jL                  jV                  _(        t               | _,        t[        |       | _.        t_        |       | _0        | j                  d       tb        jd                  s| jg                  d       | ji                  d| jj                         | ji                  d| jl                         | ji                  d| jn                         | ji                  d	| jp                         | js                          | ju                          y )
NTr   r   Fru   z
parent-setdrawzquery-tooltipzconfigure-event);r   DrawingArear,   r   r   r   set_app_paintabler   r   _opacity_last_click_time_last_click_keyr   _outside_click_timer_outside_click_detected_outside_click_num_outside_click_button_mask_outside_click_start_time_long_press_timerr#   _auto_release_timer
_key_popupdwell_timer	dwell_keylast_dwelled_keyrK   inactivity_timerr   touch_handlestouch_handles_hide_timerr   touch_handles_fadetouch_handles_auto_hide_window_aspect_ratiord   _hide_input_line_timer_transition_timerr   _transition_stater   rw   r`   r   r   _configure_timerLanguageMenu_language_menuSuggestionMenu_suggestion_menur2   rT   set_has_tooltipconnect_on_parent_set_on_draw_on_query_tooltip_on_configure_event_update_double_click_timeshowr(   s     r+   r,   zKeyboardWidget.__init__&  s*     &$--d3D(+D!t$6 !#$)G!',$"#*+')-&!&#3H#=  $ / 5)^(-%"++'+$$(!&8&B#!&!0!2/2&&,.1%%+),  &),  & %*40 .t 4 	t$   &\T-@-@AVT]];_T-C-CD&T-E-EF&&(		r.   c                 d   | j                          | j                  j                          | j                  j                          | j                  j                          | j
                  j                          | j                  j                          | j                  j                          | j                          | j                  j                          | j                          | j                          | j                          t        j                  |        t        j                  |        y r%   )stop_system_dragr   r1   r   r   r   r   r   stop_click_pollingr   close_key_popupinvalidate_keysinvalidate_shadowsr   cleanupr   rY   s    r+   r   zKeyboardWidget.cleanupi  s    
 	 	$$&%%**,##%""$##%  %%'!""$ 	!4 4 r.   c                 .    t        j                  |        y)z( called when the layout has been loaded N)r   on_layout_loadedrY   s    r+   r   zKeyboardWidget.on_layout_loaded  s    ##D)r.   c                     | j                         }|r,| j                  j                  |       | j                          y y r%   )rN   r   
set_windowupdate_window_handles)r)   widget
old_parentwins       r+   r   zKeyboardWidget._on_parent_set  s8    !!#))#.&&( r.   c                 R    | j                   |k7  r|| _         | j                          yy)3 Override deprecated Gtk function of the same name N)r   r   )r)   opacitys     r+   set_opacityzKeyboardWidget.set_opacity  s#    ==G##DMKKM $r.   c                     | j                   S )r   )r   rY   s    r+   get_opacityzKeyboardWidget.get_opacity  s    }}r.   c                    | j                         }|sJ t        j                         }| j                         }|r;|j	                         r+| j
                  j                         r| j                  d       | j                  |dd       | j                  dd       | j                          | j
                  j                         r| j
                  j                  d       |j                  |       y )Ng?ru   皙?TF)rN   r2   is_visible_on_startrO   rP   r   rV   r   transition_visible_tor]   r^   ra   set_visible)r)   r   r   rU   s       r+   set_startup_visibilityz%KeyboardWidget.set_startup_visibility  s    !!#s ,,. "f**,  ++-T" 	""7C5!!$,    ++-!!2259 	 r.   c                     | j                  ||      rb| j                  t        dd||             | j                          |j	                         }|r"|j                         }| j                  |       y y y Nr   )is_new_layout_sizer   r   r   
get_windowcairo_createrender)r)   rR   r   r   r   contexts         r+   pre_render_keyszKeyboardWidget.pre_render_keys  sl    ""1a(tAq!Q/0&&(##%C**,G$  )r.   c                 j    | j                   j                  |k7  xs | j                   j                  |k7  S r%   )r   r   r   )r)   r   r   s      r+   r
  z!KeyboardWidget.is_new_layout_size  s4    !!Q& '!!Q&	'r.   c                 T    | j                   j                  | j                               S )z$ Canvas rect excluding resize frame )r   deflateget_frame_widthrY   s    r+   get_canvas_content_rectz&KeyboardWidget.get_canvas_content_rect  s"    ''(<(<(>??r.   c                 n    | j                         }|st        dddd      S |j                  j                  S )zA Rect with aspect ratio of the layout as defined in the SVG file r   r   )
get_layoutr   r  log_rect)r)   layouts     r+   r   z#KeyboardWidget.get_base_aspect_rect  s2    "1c3''~~&&&r.   Nc                 ^   | j                         }|sy |0t        dd| j                         | j                               | _        n|| _        | j                         }|j                          | j                  || j                               }|j                  |       | j                          y r	  )r  r   get_allocated_widthget_allocated_heightr   r  update_log_rect!_get_aspect_corrected_layout_rectr   do_fit_inside_canvason_layout_updated)r)   r   r  rp   s       r+   r   zKeyboardWidget.update_layout  s    " #Aq$($<$<$>$($=$=$? AD  +D++- 55$++-/##D) 	 r.   c                    | j                         j                         }t        j                  |      }t        j                  }t        j
                  t        j                  k(  }d}d}|r|rt        j                         }n9t        j                         r%t        j                  |      r| j                         }|j                  ||      }	|re|rc|j                  |	j                  z
  }
t        j                         }|4|
d|z  kD  r,|xj                  |z  c_        |xj                  |z  c_        d}|j                  |	|      }|S )z
        Aspect correction specifically targets xembedding in unity-greeter
        and gnome-screen-saver. Else we would potentially disrupt embedding
        in existing kiosk applications.
              ?)r   d   r   ru   )rN   get_orientation_config_objectr2   "is_keep_frame_aspect_ratio_enabledrT   launched_byLAUNCHER_UNITY_GREETERget_xembed_aspect_change_rangeis_docking_enabledis_dock_expandedr   resize_to_aspect_ranger   !get_xembed_unity_greeter_offset_xr   
align_rect)r)   rp   r   orientation_cokeep_aspect
xembeddingunity_greeterx_alignaspect_change_rangerapaddingoffsets               r+   r  z0KeyboardWidget._get_aspect_corrected_layout_rect  s    ,,.LLN??O__
**f.K.KK&&,&K&K&M#++-)).9&*&J&J&L#,,-=-@BB&&244-AAC %V+FFf$FFFf$F!G??2w/Dr.   c                 *   t         j                         }t        j                  | | j	                                t        j
                  | |       | j                  j                  | j	                  d             | j                  j                  |       y)z8 Tell WindowManipulator about the active resize handles TN)r2   r(  r   set_drag_handles_get_active_drag_handleslock_x_axisr   set_active_handles)r)   dockings     r+   r   z$KeyboardWidget.update_window_handles  sp    ++- 	**41N1N1PQ%%dG4 	--d.K.KD.QR&&w/r.   c                    | j                  d       | j                          | j                  j                         r| j                  j	                  d       n| j                  j                          | j                          y)z
        Updates transparencies in response to user action.
        Temporarily presents the window with active transparency when
        inactive transparency is enabled.
        TFN)r]   r^   r   rV   ra   r1   r   rY   s    r+   update_transparencyz"KeyboardWidget.update_transparency  s_     	!!$'   ++-!!2259!!&&(r.   c                     | j                   j                         r7| j                   j                  d       | j                   j                  d       yy)z6 extend active transparency, kick of inactivity_timer TFN)r   rV   ra   rY   s    r+   touch_inactivity_timerz%KeyboardWidget.touch_inactivity_timer,  sA      ++-!!2248!!2259 .r.   c                 |    | j                   j                         r"| j                  d       | j                          y y r0   )r   rV   r]   r^   rY   s    r+   update_inactive_transparencyz+KeyboardWidget.update_inactive_transparency2  s3      ++-%%e,""$ .r.   c                 h    t         j                  j                         j                  d      | _        y)z; Scraping the bottom of the barrel to speed up key presses zgtk-double-click-timeN)r   Settingsget_defaultget_property_double_click_timerY   s    r+   r   z(KeyboardWidget._update_double_click_time7  s%    "%,,":":"<%&=> 	r.   c                    d}| j                   }| j                         }|s| j                          t        j                  ry|r|s|j                          t        j                         r|| j                  }d}d}|r|j                         }|}	|j                         }
|j                         }|r|j                         n|
}||	k7  s|r|r|}|j                         }n|j                         }|
}|j                  |j                  _        |j                  }|j                  |j                  _        |j                  }|| j!                  |j                  ||      z  }|| j!                  |j                  ||      z  }n|}||rd}n| j"                  }|| j%                  |j&                  ||      z  }||_        |S )NFru   T)r   rN   r   r2   rT   stop_auto_positioningr(  TRANSITION_DURATION_SLIDE
is_visibleget_docking_hideout_rectupdate_docking_monitorget_visible_rectget_rectr   rw   r   _init_transition TRANSITION_DURATION_OPACITY_HIDE_init_opacity_transitionr   r   )r)   r   opacity_durationslide_durationresultstater   opacity_visiblevisible_beforevisible_laterhideout_old_monmon_changedhideout_new_mon
begin_rectend_rectr   r   s                    r+   r  z$KeyboardWidget.transition_visible_to<  s   &&!!#   " ?? w%%'$$&%!%!?!?""O!$!1!("%">">"@!88:#. #&">">"@4C   "]2%4
#&#7#7#9%(\\^
#2$.LLEGGM$,JJA$.LLEGGM$,JJAd33EGGQOOFd33EGGQOOF&O##& #'#H#H $//0@B 	B")r.   c                     t         j                  ry||rd}nd}| j                  | j                  j                  ||      S )zn
        Transition active state for inactivity timer.
        This ramps up/down the window opacity.
        Fg333333?rg   )r2   rT   rQ  r   r`   )r)   r`   r|   s      r+   r]   z#KeyboardWidget.transition_active_to  sG     ??,,T-C-C-J-J-3X? 	?r.   c                    d}| j                   }| j                  }t        j                  ry| j	                         }|rF|j                         }|j                  |j                  _        |j                  |j                  _        || j                  |j                  ||      z  }|| j                  |j                  ||      z  }|S r0   )
r   TRANSITION_DURATION_MOVEr2   rT   rN   rN  r   rw   r   rO  )r)   r   r   rT  rU  r|   r   r\  s           r+   transition_position_toz%KeyboardWidget.transition_position_to  s    &&00 ??!!#J&LLEGGM&LLEGGM$''H==$''H==r.   c                     | j                   }|j                  |j                  _        |j                  |j                  _        |j                  |j                  _        |j                  |j                  _        y)zu
        Update transition variables with the actual window position.
        Necessary on user positioning.
        N)r   r   rw   r   ry   )r)   rp   rU  s      r+   sync_transition_positionz'KeyboardWidget.sync_transition_position  sM    
 &&#vv#vv#vv#vvr.   c                 |    | j                         }|r|j                         sd}|rdnd}| j                  |||      S )Nru   r   )rO   rP   rO  )r)   r   ry   r|   rU   s        r+   rQ  z'KeyboardWidget._init_opacity_transition  sA     "&..0H*s$$S,AAr.   c                 H    |j                   |k7  r|j                  ||       yyNTF)ry   r   )r)   r   ry   r|   s       r+   rO  zKeyboardWidget._init_transition  s&    |+  x8r.   c                     t         j                  ry | j                  j                         }|dk(  r| j	                          y | j
                  j                  d| j                         y )Nru   g{Gz?)r2   rT   r   r   _on_transition_stepr   r6   )r)   r|   s     r+   r^   z KeyboardWidget.commit_transition  sO    ??))::<s?$$&""((t/G/GHr.   c                    | j                   }|j                          |j                         }t        j                  j                         }t        j                  j                         }d}||j                  j                  ||z
  z  z   }||j                  j                  z  }| j                         }|r7| j                  |       |j                         }|j                  }	t        |j                  j                        }
t        |j                   j                        }|j#                         \  }}|
|k7  s||k7  r|j%                  |
|       |xs |	xr | xs |	xr |}|j                         |k7  rc|j'                  |       |s5| j(                  j+                         r| j(                  j-                  d       | j.                  j1                  |       |r|j3                  ||	       | S )Nru   F)r   r   r   r2   rR   get_active_opacityget_inactive_opacityr`   rw   r   rN   r   rJ  r   intr   r   get_position
repositionr  r   rV   ra   r   r6   on_transition_done)r)   rU  r}   active_opacityinactive_opacityinvisible_opacityr   rR   rW  rX  r   r   wxwyr   s                  r+   rh  z"KeyboardWidget._on_transition_step  s   &&!MMO"MM<<>"MM>>@#ell&8&8"%55'7 75==&&&$$&W%#..0N"44M EGGMM"AEGGMM"A((*FBBw!r'!!!Q' &6DH -#,   "g-""7+
 ((335))::5A ((..w7)).-Hxr.   c                 J    | j                         }|r|j                         S dS )z+ is the keyboard window currently visible? F)rN   rJ  r)   rR   s     r+   rJ  zKeyboardWidget.is_visible  s&    $$&&,v  "7%7r.   c                     | j                  |d       |rG| j                  j                         r-| j                  dd       | j                  j	                  d       | j                          y)z+ main method to show/hide onboard manually ru   TFN)r  r   rV   r]   ra   r^   )r)   r   s     r+   r  zKeyboardWidget.set_visible  sX    ""7C0 t,,779%%dC0!!2259 r.   c                 J    | j                         }|r|j                          yy)z2 Raise the toplevel parent to top of the z-order. N)rN   raise_to_toprv  s     r+   ry  zKeyboardWidget.raise_to_top  s$    $$&! r.   c                 J    | j                         }|r|j                          yy)z  auto-show, start repositioning N)rN   auto_positionrv  s     r+   r{  zKeyboardWidget.auto_position  s$    $$&  " r.   c                 J    | j                         }|r|j                          yy)z4 auto-show, stop all further repositioning attempts N)rN   rH  rv  s     r+   rH  z$KeyboardWidget.stop_auto_positioning  s$    $$&((* r.   c                    | j                   j                         s | j                  st        j	                         rN| j
                  j                  d| j                         d| _        t        j                         | _
        d| _        y y )Ng{Gz?Fr   )r*   has_latched_sticky_keysr   r2   are_word_suggestions_enabledr   r6   _on_click_timerr   rz   r   r   rY   s    r+   start_click_pollingz"KeyboardWidget.start_click_polling  sd    ==002??..0%%++D$2F2FG+0D(-1YY[D*&'D#	 1r.   c                 8    | j                   j                          y r%   )r   r1   rY   s    r+   r   z!KeyboardWidget.stop_click_polling(  s    !!&&(r.   c                    t        j                         }|j                         \  }}}}|t        z  rd| _        || _        n0| j                  r#d| _        t        j                  d       | j                          | j                  | j
                        }| xj                  dz  c_
        d}|dk7  r,| j                          | j                  j                  |       y|dk(  rU| j                  dk(  rFt        j                  j                   s| j                          | j                  j                  |       y| j                  |k\  r*| j                          | j                  j#                          yt%        j$                         | j&                  z
  dkD  r+| j                          | j                  j#                          yy)z1 poll for mouse click outside of onboards window TFzclick polling: outside click      g      >@)r
   get_default_root_windowget_pointerBUTTON123_MASKr   r   _loggerdebugr   _get_button_from_maskr   r   r*   on_outside_clickr2   wpdelayed_word_separators_enabledon_cancel_outside_clickrz   r   )r)   rootwindunnor   r   maskbutton
max_clickss           r+   r  zKeyboardWidget._on_click_timer+  sx   --/#//1q!T. +/D(.2D+))+0D(MM89  " **4+J+JK  ##q(#J{'')..v6  1!8!8A!=yy@@++-..v6 	 ((J6'')557 99;777$>##%MM113r.   c                     t        t        j                  j                  t        j                  j                  t        j                  j
                  f      D ]  \  }}| |z  s|dz   c S  y)Nr  r   )	enumerater
   ModifierTypeBUTTON1_MASKBUTTON2_MASKBUTTON3_MASK)r  ibits      r+   r  z$KeyboardWidget._get_button_from_maskZ  sd    !1!1!>!>!$!1!1!>!>!$!1!1!>!>!A B 	FAs cz1u		
 r.   c                 "    | j                         S   Overload for WindowManipulator )rN   rY   s    r+   get_drag_windowzKeyboardWidget.get_drag_windowc  s    ""$$r.   c                 *    t         j                         S r  )r2   get_drag_thresholdrY   s    r+   r  z!KeyboardWidget.get_drag_thresholdg  s    ((**r.   c                 j    | j                         }|r|j                          | j                  d       y)r  TN)r  on_user_positioning_begingrab_xi_pointerrv  s     r+   on_drag_initiatedz KeyboardWidget.on_drag_initiatedk  s-    %%',,.T"r.   c                     | j                         rt        j                  | _        | j                  j                          y r%   )is_resizingr   MINIMALr   r*   hide_touch_feedbackrY   s    r+   on_drag_activatedz KeyboardWidget.on_drag_activatedr  s)    DI))+r.   c                     | j                  d       t        j                  |        | j                         }|r|j	                          | j                          y)r  FN)r  r   r   r  on_user_positioning_done	reset_lodrv  s     r+   r   zKeyboardWidget.on_drag_donew  sE    U#$11$7%%'++-r.   c                    d}t         j                         rn| j                  j                  dg      }|D cg c]  }|j	                         s| }}|s<| j                  j                  dg      }|D cg c]  }|j	                         s| }}|D ](  }|j                         }|s|}|j                  |      }* || j                  }|S c c}w c c}w )z
        Returns the bounding rectangle of all move buttons
        in canvas coordinates.
        Overload for WindowManipulator
        NmoveRTRN)r2   r(  r*   find_items_from_idsis_path_visiblerj   unionr   )r)   boundskeyskro   rs         r+   get_always_visible_rectz&KeyboardWidget.get_always_visible_rect  s     $$&==44fX>D#;!q'8'8':A;D;}}88&B#'?a1+<+<+>?? -..0F#\\!_F- >%%F < @s   CC6CCc                 l    | j                   j                  |      }|t        j                  | |      }|S r  )r   hit_testr   hit_test_move_resize)r)   rh   hits      r+   r  z#KeyboardWidget.hit_test_move_resize  s4      ))%0;#88uEC
r.   c                     | j                  | j                         | j                               r<| j                          | j	                          | j                  | j                         y y r%   )r
  r  r  r   r   r   r   )r)   r   	user_datas      r+   r   z"KeyboardWidget._on_configure_event  sX    ""4#;#;#=#'#<#<#>@ //1&&tyy1	@r.   c                    | j                   j                          | j                          |j                  t        z  ry |j                         }|j                         }|t        j                  j                  k7  r5| j                  j                         r| j                  j                  d       | j                          y r\   )r*   on_activity_detectedr   rU  r  get_source_device
get_sourcer
   InputSourceTOUCHSCREENr   rV   ra   r   r)   r   eventsource_devicesources        r+   on_enter_notifyzKeyboardWidget.on_enter_notify  s    **,&&( ;;' //1))+S__000 $$//1%%66t< 	!r.   c                    |j                   t        z  ry | j                  j                  |j                  |j
                  f      ry | j                          | j                          | j                          |j                         }|j                         }|t        j                  j                  k7  r5| j                  j                         r| j                  j!                  d       | j#                          y r0   )rU  r  r   rk   r   r   stop_dwellingreset_touch_handlesr  r  r  r
   r  r  r   rV   ra   reset_drag_cursorr  s        r+   on_leave_notifyzKeyboardWidget.on_leave_notify  s     ;;' ++UWWegg,>?  " 	  " //1))+S__000$$//1%%66u= 	 r.   c                 ~    t         j                  s-| xr t         j                          }| j                  ||       yy)z/ Set/reset the cursor for frame resize handles N)r2   rT   has_window_decorationset_drag_cursor_at)r)   rh   hit_keyallow_drag_cursorss       r+   do_set_cursor_atzKeyboardWidget.do_set_cursor_at  s>    %, "D%+%A%A%C!C ##E+=> r.   c                    | j                   j                          | j                          | j                          | j	                          |j                         r5| j                  j                         r| j                  j                  d       |j                  }d}d}| j                  j                  rZ| j                  j                  |      }| j                  j                  |       || j                          n| j                  d       || j!                  |      }|| j#                  d       nR|r1|j$                  dk(  r"| j#                  d       | j'                          n| j#                  t(        j*                         |;t(        j-                         s't(        j.                  st1        j2                  | |      ryt(        j4                  j6                  ry||_        ||_        |r| j<                  |k7  s&|j>                  | j@                  z
  | jB                  kD  r|tD        jF                  |_$        | jK                  |       t(        j                   jL                  }|j$                  dk(  r|dz  }| jN                  jQ                  || jR                  |       n&tD        jT                  |_$        | jK                  |       || _        |j>                  | _         y)z Button press/touch begin TNFr  rg   )+r*   r  r   r  r   is_touchr   rV   ra   rh   r   r`   r  set_pressedstop_touch_handles_auto_hideshow_touch_handlesget_key_at_locationenable_drag_protectionidreset_drag_protectionr2   drag_protectionr  rT   r   handle_pressscannerenabled
active_keyinitial_active_keyr   rz   r   rF  r   CLICK
event_typekey_downlong_press_delayr   r6   _on_long_pressDOUBLE_CLICK)r)   rn   rh   ro   
hit_handler8   s         r+   on_input_sequence_beginz&KeyboardWidget.on_input_sequence_begin  sZ   **,!   ++-!!2248 
$$++44U;J**:6%113 ''. **51C !''.SVVv% ''-&&(''(>(>? ;++- --dH= >>!! "&)###s*}}t444t7N7NN '0oo#h' 8866V#SLE&&,,U-1-@-@(L
 '0&<&<#h'#&D $,MMD!r.   c                 F   |j                   sy| j                  }|r|j                  ||j                         y|j                  }d}d}| j
                  j                  r6| j
                  j                  |      }| j
                  j                  |       || j                  |      }|j                  t        z  rd}t        j                  | ||       | j                         r| j                          |j                   }| j#                         sH||k7  rB| j                          | j%                  |      r |r|j&                  s| j                  s||_        | j)                  ||       n|| j+                          | j,                  j                  |       |r|j.                  r| j0                  j2                  j5                  |      }|rv|j7                         rf| j9                         sV| j;                  |      sEt<        j>                  j@                  s+t<        jB                  jD                  s| jG                  |       | jI                  ||       | jJ                  r| jJ                  |k7  s| jL                  r!| jL                  |k7  r| jO                          yyy)z Pointer motion/touch update NT)fallback)(primaryr   redirect_sequence_updateon_input_sequence_updaterh   r   r`   r  set_prelightr  rU  r  r   rq   is_drag_activestop_long_pressr  is_drag_initiated _overcome_initial_key_resistance	activatedkey_down_updatestart_touch_handles_auto_hider   	sensitiver*   button_controllersget	can_dwellis_dwellingalready_dwelledr2   r  r  lockdowndisable_dwell_activationstart_dwellingr  r   r   cancel_dwelling)	r)   rn   popuprh   r  r  r  r  
controllers	            r+   r  z'KeyboardWidget.on_input_sequence_updateB  s6    **8+0+I+IK 
$$++44U;J++J7 ..u5G>>N* H ++D(xP ""$$$& ",,J))+W$$$&88B"**>*>*1H'((:> %224 ''55h?   !]]==AA'J
*"6"6"8'')++G4~~--??''0!!%1 >>dnn7  T%:%:g%E  " &F r.   c                 V   | j                   }|r,|j                         r|j                  ||j                         |j                  }|r+t
        j                  j                  s| j                  |       | j                          | j                          |j                  }| j                  |      }| j                  ||       | j                          | j                          |j!                         r7| j"                  j%                         r| j"                  j'                  d       yyy)z Button release/touch end FN)r   
got_motionredirect_sequence_endon_input_sequence_endr  r2   r  r  key_up	stop_dragr  rh   r  r  r  r  r  r   rV   ra   )r)   rn   r  r  rh   r  s         r+   r	  z$KeyboardWidget.on_input_sequence_end  s     ''(-(C(CE ((
~~%%KK! **51eW- 	  "**,   ++-!!2259 . r.   c                     | j                          t        j                  | j                         v r2|r0| j	                         s | j                          | j                          yr\   )r  r   MOVEget_drag_handlesr  r  start_move_windowr)   num_touchess     r+   on_drag_gesture_beginz$KeyboardWidget.on_drag_gesture_begin  sM    ;;$//11%%'##%""$r.   c                 $    | j                          yr\   )stop_move_windowr  s     r+   on_drag_gesture_endz"KeyboardWidget.on_drag_gesture_end  s    r.   c                 0    |dk(  r| j                          yy)N   TF)r  r  s     r+   on_tap_gesturezKeyboardWidget.on_tap_gesture  s    !##%r.   c                 t    | j                   j                  |j                  | |j                        }||_        y r%   )r*   key_long_pressr  r  cancel_key_action)r)   rn   long_presseds      r+   r  zKeyboardWidget._on_long_press  s0    }}33H4G4G48(//K%1"r.   c                 8    | j                   j                          y r%   )r   r1   rY   s    r+   r  zKeyboardWidget.stop_long_press  s    ##%r.   c                     | j                   j                  |j                  | |       | j                  j	                          y r%   )r*   r  r  r   r6   r)   rn   s     r+   r  zKeyboardWidget.key_down  s0    x22D(C  &&(r.   c                     |r|j                   rJ | j                  j                  || |d       | j                  j                  |j                  | |d       y r0   )r  r*   r
  r  r  )r)   rn   old_keys      r+   r  zKeyboardWidget.key_down_update  sI    '"3"343WdHe<x22D(EJr.   c                 j    | j                   j                  |j                  | ||j                          y r%   )r*   r
  r  r  r  s     r+   r
  zKeyboardWidget.key_up  s,    X00$!)!;!;;	=r.   c                     | j                   d uS r%   )r   rY   s    r+   r  zKeyboardWidget.is_dwelling  s    >>T))r.   c                     | j                   |u S r%   )r   r)   ro   s     r+   r  zKeyboardWidget.already_dwelled  s    $$++r.   c                     | j                          || _        || _        |j                          t	        j
                  d| j                        | _        y )N2   )r  r   r   r  r	   timeout_add_on_dwell_timerr   r%  s     r+   r  zKeyboardWidget.start_dwelling  sD     #++B0D0DEr.   c                 2    | j                          d | _        y r%   )r  r   rY   s    r+   r  zKeyboardWidget.cancel_dwelling  s     $r.   c                     | j                   rdt        j                  | j                          d | _         | j                  | j                  g       | j                  j                          d | _        y y r%   )r   r	   source_remover   r   r  rY   s    r+   r  zKeyboardWidget.stop_dwelling  sV    t//0#DKK()NN((*!DN r.   c                    | j                   r| j                  | j                   g       | j                   j                         r| j                   }| j                          t	               }d|_        t        j                  |_        ||_	        |j                         j                         |_        t        | |j                        |_        | j                  |       | j!                  |       yy)Nr   FT)r   r   r   r  r   r  r   DWELLr  r  get_canvas_rect
get_centerrh   r   
root_pointr  r
  )r)   ro   rn   s      r+   r)  zKeyboardWidget._on_dwell_timer  s    >>KK()~~%%'nn""$(?"#&/oo#&)#!$!4!4!6!A!A!C3D(..I # h'H%r.   c                    t         j                  r| j                         s| j                         s| j	                  ||f      }|r|j
                  rst        j                         }|j                         \  |_	        |_
        |_        |_        |j                  |       |j                  t        |j
                               yyrf  )r2   show_tooltipsr  last_event_was_touchr  tooltipr
   	Rectangler/  r   r   widthheightset_tip_areaset_text_)r)   r   r   r   keyboard_moder5  ro   r  s           r+   r   z KeyboardWidget._on_query_tooltip  s    %%'((***Aq62Cs{{MMO.1.A.A.C+QS!'18$$Q'  3;;0r.   c                 R   |rt         j                  j                  ry|r| j                  j	                  d       | j                  j                  d       d| j                  _        || _        t        |       \  }}| j                  j                  ||       | j                          |r| j                          d\  }}n| j                          d\  }}| j                  j                  |k7  r:d| j                  _        | j                  j!                  ||d| j"                         yy)z`
        Show/hide the enlarged resize/move handels.
        Initiates an opacity fade.
        NT)ru   r   )r   ru   g?g?)r2   r   disable_touch_handlesr   r  r  r`   r   r   set_monitor_dimensionsr   r  r  r   ry   	time_stepfade_to_on_touch_handles_opacity)r)   r   	auto_hidesizesize_mmr6   ends          r+   r  z!KeyboardWidget.show_touch_handles  s    
 FOO99++D1**40(,D%+4D(248MD'55dGD//1224!JE3--/!JE3""//3605D##-##++E3&*&D&DF 7r.   c                     | j                   j                  r7| j                   j                  d        | j                   j                  d        y y r%   )r   r`   r  r  rY   s    r+   r  z"KeyboardWidget.reset_touch_handles4  s>    $$++D1**40 %r.   c                     | j                   j                  r5| j                  r(| j                  j	                  d| j
                  d       yyy)z. (re-) starts the timer to hide touch handles r  FN)r   r`   r   r   r6   r  rY   s    r+   r  z,KeyboardWidget.start_touch_handles_auto_hide9  sD    $$)E)E))//040G0GP *F$r.   c                 8    | j                   j                          y)z' stops the timer to hide touch handles N)r   r1   rY   s    r+   r  z+KeyboardWidget.stop_touch_handles_auto_hide?  s    %%**,r.   c                    |r|dk  rd| j                   _        || j                   _        	 | j                   j                  D ]q  }t        | j                  j                  dz        |j                  t        j                  t        j                  t        j                  fv k7  sb|j                          s |r"t        j                  | j                  dd       y y )Ng?Fr  r   )r   r`   r   r   handlesr;   r   	iterationr  r   r  NORTHSOUTHr	   idle_addrB  )r)   r   r}   handles       r+   rB  z(KeyboardWidget._on_touch_handles_opacityC  s    GcM(-D%%,"  ,,44 $//99A=>II&++v||V\\!JJLMMO$
 d<<c5I r.   c                 V    | j                   j                  | j                                y r%   )r   update_positionsr   rY   s    r+   r   z-KeyboardWidget.update_touch_handles_positionsZ  s    ++D,H,H,JKr.   c                 h   |j                          t        j                  | ||      }| j                  j                  rJ|rt
        j                  nd}| j                  j                  |       | j                  j                  |       |j                          |j                  | j                         y r	  )
push_groupr   r   r   r`   r2   CORNER_RADIUSset_corner_radiuspop_group_to_sourcepaint_with_alphar   )r)   r   r  	decoratedcorner_radiuss        r+   r   zKeyboardWidget._on_draw]  s    OOD&':	 $$4=F001M00?##G,##%  /r.   c                     d}|j                   }|rn||j                  u r`|j                         }t        |j                  |j
                        |z  }|j                  |      }|j                  |j                        ryy)z
        Drag-select: Increase the hit area of the initial key
        to make it harder to leave the the key the button was
        pressed down on.
        r  FT)	r  r  rj   r5   r   r   inflaterk   rh   )r)   rn   #DRAG_SELECT_INITIAL_KEY_ENLARGEMENTr  rp   r  s         r+   r  z/KeyboardWidget._overcome_initial_key_resistancek  sr     /2+((
*(C(CC446DDFFDFF#&IIA<<?D##HNN3r.   c                 "    | j                         S r%   )
get_parentrY   s    r+   rN   zKeyboardWidget.get_kbd_window|  s      r.   c                 j    | j                         j                         }t        j                  |       S z Overload for LayoutView )rN   r#  r2   r)  r)   cos     r+   can_draw_framezKeyboardWidget.can_draw_frame  s-      "@@B**2...r.   c                 h    | j                         j                         }t        j                  |      S ra  )rN   r#  r2   *is_keep_docking_frame_aspect_ratio_enabledrb  s     r+   can_draw_sidebarsz KeyboardWidget.can_draw_sidebars  s*      "@@B@@DDr.   c                 *   t         j                  rt         j                  S t         j                         ry| j	                         j                         }t         j                  |      ryt         j                  j                  ryt         j                  S )z= Width of the frame around the keyboard; canvas coordinates. ru   r   g      @)	r2   rT   UNDECORATED_FRAME_WIDTHr  rN   r#  r)  rR   transparent_backgroundrb  s     r+   r  zKeyboardWidget.get_frame_width  sh    ??111'')  "@@B""2&==//---r.   c                      y)N
   rI   rY   s    r+   get_hit_frame_widthz"KeyboardWidget.get_hit_frame_width  s    r.   c                    t         j                  rd}|S t         j                         r| j                         j	                         }|rNt
        j                  t
        j                  t
        j                  t
        j                  t
        j                  f}n!t
        j                  }nt
        j                  }|sFt         j                  j                  }t        t        |      j!                  t        |                  }|S )NrI   )r2   rT   r(  rN   get_dock_expandr   rM  rN  WESTEASTr  RESIZE_MOVErR   window_handlestuplesetintersection)r)   all_handlesrK  expandconfig_handless        r+   r8  z'KeyboardWidget._get_active_drag_handles  s    ??G$ ! ((*,,.>>@%||V\\%{{FKK%{{,G %00G ,,!'!=!=G 9 9#n:M NOr.   c                     |t         j                  t         j                  fv rBt        j	                         r.| j                         j                         rt        j                  S t        j                  S r%   )
r   rp  rq  r2   r(  rN   ro  r   ASPECT_RATIONORMAL)r)   rP  s     r+   get_handle_functionz"KeyboardWidget.get_handle_function  sP    fkk6;;//$$& 002$111$$$r.   c                     | j                   j                  g d      }g }|D ]W  }|j                         }t        | |      }t        j
                  }|r|dk7  r|j                  |      }|j                  |       Y |S )zk
        Returns bounding rectangles of all click type buttons
        in root window coordinates.
        )singleclicksecondaryclickmiddleclickdoubleclick	dragclickr   )r*   r  rj   r   r2   window_scaling_factorscaleappend)r)   r  rectsro   r  r  s         r+   get_click_type_button_rectsz*KeyboardWidget.get_click_type_button_rects  s    
 }}00 2? @
  
	C**,A*43A 00E#GGENLLO
	 r.   c                 Z   |j                         }| j                         j                  |j                  |j                        \  }}| j                         j                  |j                  |j
                  z   |j                  |j                  z         \  }}t        ||||z
  ||z
        S )zS
        Returns bounding rectangles of key in in root window coordinates.
        )rj   r  get_root_coordsr   r   r   r   r   )r)   ro   r  x0y0x1y1s          r+   get_key_screen_rectz"KeyboardWidget.get_key_screen_rect  s     &&("22133<B"22133934339>BBBGRV,,r.   c                 ,   | j                         }d }| j                         j                         }t        j	                  |      rB|j                         }|j                  t        |j                        z  }|j                         }| j                  |k7  r~| j                         }|rkt        j                         }||j                  | |d       n9|x|_        |_        |j                  | |t        j                   j"                         || _
        y y y r	  )r  rN   r#  r2   #is_keep_window_aspect_ratio_enabledget_border_rectr   floatr   get_log_aspect_ratior   r
   Geometryset_geometry_hintsr   
max_aspectWindowHintsASPECT)r)   r  aspect_ratiorc  r  rR   geoms          r+   r  z KeyboardWidget.on_layout_updated  s     "  "@@B55b9--/H#::hjj(99L!668L$$4((*F||~'--dD!<8DDDOdo--dD#//:P:PQ,8)  5r.   c                 R   t         j                  dj                  t        j                  j                         j                  d                   t        j                          | j                          | j                  j                          | j                  j                          y)z
        When the systems font dpi setting changes, our pango layout object
        still caches the old setting, leading to wrong font scaling.
        Refresh the pango layout object.
        z5Refreshing pango layout, new font dpi setting is '{}'zgtk-xft-dpiN)r  infoformatr   rC  rD  rE  r   reset_pango_layoutinvalidate_label_extentsr*   r   rB   rY   s    r+   refresh_pango_layoutsz$KeyboardWidget.refresh_pango_layouts  sp     	L002??NO	Q 	 %%'##%'')r.   c                    | j                  | j                               }t        j                  || j	                         |      } |j
                  |  | j                  ||       || _        | j                  j                          y)z/
        Popup with alternative chars.
        N)
_create_key_popuprN   r   buildget_color_scheme
set_layout_show_key_popupr   r*   r  )r)   ro   alternativesr  rT  s        r+   show_popup_alternative_charsz+KeyboardWidget.show_popup_alternative_chars  ss     &&t':':'<=*U3 5 5 7G&!UC())+r.   c                    | j                  | j                               }t        j                  || j	                         |      } |j
                  |  | j                  ||       || _        | j                  j                          y)z5
        Popup with predefined layout items.
        N)
r  rN   r   r  r  r  r  r   r*   r  )r)   ro   r  r  rT  s        r+   show_popup_layoutz KeyboardWidget.show_popup_layout  ss     &&t':':'<=U3 5 5 7A&!UC())+r.   c                 `    | j                   r"| j                   j                          d | _         y y r%   )r   destroyrY   s    r+   r   zKeyboardWidget.close_key_popup  s%    ??OO##%"DO r.   c                     t        | j                  | j                        }| j                  |_        |j	                  |       |j                  | j                                |S r%   )r   r*   r   supports_alphaset_transient_forr   r  )r)   parentr  s      r+   r  z KeyboardWidget._create_key_popup!  sP    DMM4+?+?@#22'$**,-r.   c                     |j                         }t        | |      }|j                  |j                  |j                  dz  z   |j
                  dd       |j                          |S )Nr!  r   )rj   r   position_atr   r   r   show_all)r)   r  ro   r  	root_rects        r+   r  zKeyboardWidget._show_key_popup(  sX    &&(.tQ7	)++	c(99"c3	0r.   c                    t         j                  j                  |d      \  }}|t         j                  v rt        d|      }d}nt	        d      }t        d|      }| j
                  j                          t        j                  || j                         d      }|j                  t	        d      t        j                  j                         |j                  t	        d	      t        j                  j                         t         j                         r)|j                  t        j                   j"                         |j%                  t        j                  j                         t        j&                  t        j(                  j*                  d
d      }|j-                         j/                  |       |r(t        j0                  |d      }|j/                  |       t        j2                  d      }	t        j2                  ddd      }
t        j0                  t	        d      dd|	      }t        j0                  t	        d      dd|
      }t        j4                  dd      }|j7                  |dddd       |j7                  |dddd       |j7                  |	dddd       |j7                  |
dddd       |j/                  |       t         j                  j                  |d      \  }}|r|	j9                  |       |r|
j9                  |       |r|s|
j;                          n|	j;                          |j=                  d| j>                  ||	|
       |jA                          y)z( Show dialog for creating a new snippet NNzEdit snippet #{} zNew snippetz#Enter a new snippet for button #{}:r   )titletransient_forflags_Cancelz_Save snippet      )orientationspacingborder_widthru   labelxalignT)hexpand#   )r  activates_defaultwidth_charsz_Button label:)r  r  use_underlinemnemonic_widgetz	S_nippet:   r  )row_spacingcolumn_spacingr  responseN)!r2   snippetsr  _formatr;  r*   on_focusable_gui_openingr   Dialogget_toplevel
add_buttonResponseTypeCANCELOKis_force_to_topset_positionWindowPositionCENTERset_default_responseBoxOrientationVERTICALget_content_areaaddLabelEntryGridattachr:  
grab_focusr   _on_snippet_dialog_responser  )r)   
snippet_idr  textr  messagedialogbox	msg_labellabel_entry
text_entrylabel_label
text_labelgrids                 r+   show_snippets_dialogz#KeyboardWidget.show_snippets_dialog0  s    oo))*lCt(.
;EG m$ECZPG 	..0%)-):):)<!"$
 	!I,(8(8(?(?@!O,c.>.>.A.AB !!# 2 2 9 9:##C$4$4$7$78gg#//":": q2!%%c*		<IGGIii-ii48,.0
 iia(8&9'*.20;= iian'*.20:<

 xxAa8KAq!,J1a+KAq!,J1a+ oo))*lCt  '%!!#""$z4#C#C!;
	<r.   c                    |t         j                  j                  k(  rw|j                         }|j                         }t        j
                  j                  dk(  r"|j                  d      }|j                  d      }t        j                  |||f       |j                          | j                  j                          t        j                  | j                  j                         y )Nr   zutf-8)r   r  r  get_textsysversion_infomajordecoder2   set_snippetr  r*   on_snippets_dialog_closedr	   rO  on_focusable_gui_closed)r)   r  r  r  r  r  r  r  s           r+   r  z*KeyboardWidget._on_snippet_dialog_response{  s    s''***((*E&&(D%%*W-{{7+zE4=9//1 	dmm;;<r.   c                 >    | j                   j                  |||       y r%   )r   r  r)   ro   r  closures       r+   show_language_menuz!KeyboardWidget.show_language_menu  s    !!#vw7r.   c                 6    | j                   j                         S r%   )r   
is_showingrY   s    r+   is_language_menu_showingz'KeyboardWidget.is_language_menu_showing  s    ""--//r.   c                 >    | j                   j                  |||       y r%   )r   r  r  s       r+   show_prediction_menuz#KeyboardWidget.show_prediction_menu  s    ##C9r.   r%   r  )TT)F)drE   rF   rG   r`  rI  rP  r,   r   r   r   r   r  r  r  r
  r  r   r   r  r   r=  r?  rA  r   r  r]   ra  rc  rQ  rO  r^   rh  rJ  r  ry  r{  rH  r  r   r  staticmethodr  r  r  r  r  r   r  r  r   r  r  r  r  r  r	  r  r  r  r  r  r  r  r
  r  r  r  r  r  r)  r   r  r  r  r  rB  r   r   r  rN   rd  rg  r  rm  r8  r}  r  r  r  r  r  r  r   r  r  r  r  r   r  r  rI   r.   r+   r    r      s     $ $'*$AF!2*)"!H	%'@'!.%N
0:%
?
 AE>BAF?"(	&	B	I/b8
	!"#+()-^  %+#,
62":!@?RhJ#X :D2
&)K
=*,F%",F>1
P-J.L0"!/
E
.,%2-92*,,#
IV=&80:r.   c                   <    e Zd ZdZd Zd Zd
dZd Zd Zd Z	d	 Z
y)KeyMenuz Popup menu for keys c                 r    || _         | j                   j                  | _        d | _        d | _        d| _        y )Nru   )_keyboard_widgetr*   r'   _menu_closure_x_alignr)   keyboard_widgets     r+   r,   zKeyMenu.__init__  s2     /..77
r.   c                     | j                   d uS r%   )r  rY   s    r+   r  zKeyMenu.is_showing  s    ::%%r.   Nc           	      4   || _         | j                  j                          | j                  ||      }|| _        |j                  d| j                         |j                          |j                  d d | j                  ||t        j                                y )Nunmap)r  r'   r  create_menur  r   _on_menu_unmapr  r  _menu_positioning_funcr   get_current_event_time)r)   ro   r  r  menus        r+   r  zKeyMenu.popup  sw    //1V,
Wd112

4t:: : : <	>r.   c                     t               )z  Overload this in derived class )NotImplementedError)r)   ro   r  s      r+   r  zKeyMenu.create_menu  s    !##r.   c                     t        d| j                  j                         d | _        | j                  r| j	                          y y Nr!  )r   r'   r  r  r  )r)   r  s     r+   r  zKeyMenu._on_menu_unmap  s2    c4>>99:
==MMO r.   c                     t        |      dk(  r|\  }}}}n|\  }}| j                  j                  |      }|j                         |j                         f}| j	                  ||      \  }}||dfS )Nr  F)lenr
  r  r  get_menu_position)r)   paramsr  r   r   ro   r  	menu_sizes           r+   r  zKeyMenu._menu_positioning_func  s{    v;!$OD!QID#!!55c:..0..02	%%a31!U{r.   c                     |j                         |j                  |d   z
  | j                  z  z   |j                         fS r	  )leftr   r  bottom)r)   rkeyr   s      r+   r  zKeyMenu.get_menu_position  s9    yy{dffy|3t}}DD{{} 	r.   r%   )rE   rF   rG   rH   r,   r  r  r  r  r  r  rI   r.   r+   r  r    s(    &
>$r.   r  c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r   z$ Popup menu for the language button c                    | j                   }|j                  }|j                         }t        j	                         }t        |j                               }||v r|j                  |       t        j                  j                  }t        j                  j                  }	|	D 
cg c]	  }
|
|v s|
 c}
d | }t        |      j                  |      }g }|D ])  }|j                  |      }|s|j                  ||f       + t        j                         }t!        |      D ]R  \  }}t        j"                  j%                  |      }|j'                  d| j(                  |       |j                  |       T t        j                         }|j                         }|j                  |      }t        j*                  j-                  |      }|j/                  d       |j1                  |        |j'                  d| j2                  d       |j                  |       t        j4                  j7                         }|j                  |       |D ]  }|j                  |      }|st        j*                  j%                  |      }|j/                  d       |j1                  ||k(         |j'                  d| j2                  |       |j                  |        |r/t        j4                  j7                         }|j                  |       |rJt        j"                  j-                  t9        d            }|j;                  |       |j                  |       |S c c}
w )NactivateTr  zOther _Languages)r'   _languagedbget_active_lang_idr2   get_system_default_lang_idru  get_language_idsremovetyping_assistancemax_recent_languagesrecent_languages
differenceget_language_full_namer  r   MenusortedMenuItemnew_with_labelr   _on_other_language_activatedCheckMenuItemnew_with_mnemonicset_draw_as_radio
set_active_on_language_activatedSeparatorMenuItemnewr;  set_submenu)r)   ro   r  r*   
languagedbactive_lang_idsystem_lang_idlang_idsmax_mru_languagesall_mru_lang_idsr  mru_lang_idsother_lang_idsother_langslang_idname	lang_menuitemr  s                      r+   r  zLanguageMenu.create_menu  s   >>))
!446::<z2245X%OON+"44II!33DD(8K"B(N2K--/h-22<@% 	4G44W=D""D'?3	4 HHJ	#K0 	#MD'<<..t4DLLT%F%FPT"	# xxz!44600@  2248t$N*+Z!<!<bAD$$((*D# 	"G44W=D((77=&&t,> 9:Z)D)DgND!	" ((,,.DKK<<11!4F2GHDY'KKa Ls   #	M-Mc                 \    t         j                         }||k(  rd}| j                  |       y )Nr  )r2   r*  _set_active_lang_id)r)   r  rH  rA  s       r+   r;  z#LanguageMenu._on_language_activated  s+    ::<n$G  )r.   c                 L    |r| j                  |       | j                  |       y r%   )_set_mru_lang_idrM  )r)   r  rH  s      r+   r6  z)LanguageMenu._on_other_language_activated  s!    !!'*  )r.   c                 :    | j                   j                  |       y r%   )r'   set_active_lang_id)r)   rH  s     r+   rM  z LanguageMenu._set_active_lang_id  s    ))'2r.   c                     t         j                  j                  }t         j                  j                  d d  }||v r|j	                  |       |j                  d|       |d | }|t         j                  _        y r	  )r2   r-  r.  r/  r,  insert)r)   rH  r.  r/  s       r+   rO  zLanguageMenu._set_mru_lang_id  so    %77LL!33DDQG&&##G,7++,A-AB4D  1r.   N)	rE   rF   rG   rH   r  r;  r6  rM  rO  rI   r.   r+   r   r     s     .=~**
3Er.   r   c                   4     e Zd ZdZd Zd Zd Z fdZ xZS )r   z( Popup menu for word suggestion buttons c                 >    t         j                  | |       d| _        y r  )r  r,   r  r  s     r+   r,   zSuggestionMenu.__init__*  s    /r.   c                     |j                   | _        t        j                         }t        j                  j                  t        d            }|j                  d| j                  |       |j                  |       |S )Nu   _Remove suggestion…r'  )
code_choice_indexr   r2  r4  r8  r;  r   _on_remove_suggestionr  )r)   ro   r  r  rK  s        r+   r  zSuggestionMenu.create_menu.  s\     XX xxz||--a0G.HIZ!;!;SADr.   c                 x   | j                   }|j                         }|j                  || j                        \  }}|dd  }t	        | j
                  j                         |||      }|j                         }|rB|g}	|dk(  r|dd  |	z   }	|j                  |	       |j                          |j                          y y )Nr   )r'   r_  !get_prediction_choice_and_historyrX  "RemoveSuggestionConfirmationDialogr
  rN   runremove_prediction_contextinvalidate_context_uirB   )
r)   	menu_itemro   r*   wordlist
suggestionhistoryr  context_lengthr  s
             r+   rY  z$SuggestionMenu._on_remove_suggestion;  s    >>>>#66x7;7I7IK 	
G "#,3))88:j'3  !lG"!"#,0..w7 **,&&( r.   c                     |d   |j                   kD  r |j                         |j                         fS t        t        |   ||      S r	  )r   r"  r#  superr   r  )r)   r$  r   	__class__s      r+   r  z SuggestionMenu.get_menu_positionQ  sC    Q<$&& 99;-- ##'34r.   )	rE   rF   rG   rH   r,   r  rY  r  __classcell__)rh  s   @r+   r   r   '  s    2),4 4r.   r   c                   ,    e Zd ZdZd Zed        Zd Zy)r]  z& Confirm removal of a word suggestion c                    || _         d | _        d | _        t        j                  j                  | t        j                  j                  t        j                  j                  t        d             |r| j                  |       t        j                         r)| j                  t        j                  j                          dt        d      z   dz   }t#        |d      }| j%                  |       t        j&                  t        j(                  j*                  d	      }| j-                  ||      }t        j.                  j1                  d |      | _        |j3                  | j                         t        j.                  j5                  | j                  t7        d
|            | _        |j3                  | j                         |s6| j                  j9                  d       | j                  j;                  d       t        j<                  t        d      d      }|j3                  |       | j?                         j3                  |       | jA                          y )NOnboard)message_typebuttonsr  z<big>zRemove word suggestionz</big>T)preserve_tagsr  )r  r  zRemove '{}' everywhere.Fz*This will only affect learned suggestions.ru   r  )!r'   _radio1_radio2r   MessageDialogr,   MessageTypeQUESTIONButtonsType	OK_CANCELr;  r  r2   r  r  r  r  r   
set_markupr  r  r  "_get_remove_context_length_2_labelRadioButtonr5  r  new_with_label_from_widgetr  set_sensitiver:  r  get_message_arear  )r)   r  r*   rc  rd  markupr  r  s           r+   r,   z+RemoveSuggestionConfirmationDialog.__init__\  s   !""4030H0H+.??+D+D)*9 	# 	7
 ""6* !!#c007781566AvT:gg#//":":BG77
GL55dEBAA$,,8*EG LL&&u-LL##D)		"N O*-/##C(r.   c                     |r|d   nd}|r|j                  d      rt        d|       }|S |dk(  rt        d|       }|S |dk(  rt        d|       }|S t        d	| d
j                  |            }|S )a  
        Label of radio button for remove context length 2.

        Doctests:
        >>> from Onboard.KeyboardWidget import RemoveSuggestionConfirmationDialog
        >>> test = RemoveSuggestionConfirmationDialog._get_remove_context_length_2_label
        >>> def _format(msgstr, *args, **kwargs):
        ...     return msgstr.format(*args, **kwargs)
        >>> import builtins
        >>> builtins.__dict__['_format'] = _format

        >>> test("word", [])
        "Remove 'word' only where it occures at text begin."

        >>> test("word", ["word2"])
        "Remove 'word' only where it occures after 'word2'."

        >>> test("word", ["word2", "word3"])
        "Remove 'word' only where it occures after 'word2 word3'."

        >>> test("word", ["<unk>"])
        "Remove 'word' only where it occures after '<unk>'."

        >>> test("word", ["<s>"])
        "Remove 'word' only where it occures at sentence begin."

        >>> test("word", ["<num>"])
        "Remove 'word' only where it occures after numbers."
        r[  Nz<bot:z0Remove '{}' only where it occures at text begin.z<s>z4Remove '{}' only where it occures at sentence begin.z<num>z0Remove '{}' only where it occures after numbers.z-Remove '{}' only where it occures after '{}'. )
startswithr  join)rc  rd  hist0r  s       r+   rx  zERemoveSuggestionConfirmationDialog._get_remove_context_length_2_label  s    >  'D((1N&(E  e^R&(E  gN&(E
  K&(9;Er.   c                 h   | j                   }|r|j                          t        j                  j	                  |       }| j                          |r|j                          |t        j                  j                  k(  r6| j                  j                         ry| j                  j                         ryy)Nr   r  r   )r'   r  r   r  r^  r  r  r  r  rq  
get_activerp  )r)   r*   r  s      r+   r^  z&RemoveSuggestionConfirmationDialog.run  s    >>--/::>>$',,. s''***||&&(||&&(r.   N)rE   rF   rG   rH   r,   r  rx  r^  rI   r.   r+   r]  r]  Y  s%    0+Z + +Zr.   r]  )GrH   
__future__r   r   r   r  rz   mathr   r   Onboard.Versionr   gi.repositoryr	   r
   r   Onboard.TouchInputr   r   Onboard.Keyboardr   Onboard.KeyboardPopupsr   r   r   Onboard.KeyGtkr   Onboard.KeyCommonr   Onboard.TouchHandlesr   Onboard.LayoutViewr   Onboard.utilsr   r   Onboard.Timerr   r   Onboard.definitionsr   r   Onboard.WindowUtilsr   r   r   r   logging	getLoggerr  Onboard.Configr!   r2   r  r  r  r  r  r#   rK   rd   rt   r   r   r   r    r  r   r   rr  r]  rI   r.   r+   <module>r     sY  ,  A A 
   /   1 1 < ,1 1 ' & / - 6 3 9: : 
'

,
- "	 !!..!!../!!../#u #L&e &R <( (B3 3.5#4 5pv:S__&Bv:r+2 2jWE7 WEt/4W /4dp):): pr.   