| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" Test using a KeyBindings object with the traits CodeEditor.""" |
| 12 |
|
| 13 |
# ------------------------------------------------------------------------- |
| 14 |
# Imports: |
| 15 |
# ------------------------------------------------------------------------- |
| 16 |
|
| 17 |
from traits.api import HasPrivateTraits, Code, Str |
| 18 |
|
| 19 |
from traitsui.api import View, Item, Handler, CodeEditor |
| 20 |
|
| 21 |
from traitsui.key_bindings import KeyBinding, KeyBindings |
| 22 |
|
| 23 |
# ------------------------------------------------------------------------- |
| 24 |
# Define a KeyBindings object: |
| 25 |
# ------------------------------------------------------------------------- |
| 26 |
|
| 27 |
key_bindings = KeyBindings( |
| 28 |
KeyBinding( |
| 29 |
binding1='Ctrl-s', |
| 30 |
description='Save to a file', |
| 31 |
method_name='save_file', |
| 32 |
), |
| 33 |
KeyBinding( |
| 34 |
binding1='Ctrl-r', description='Run script', method_name='run_script' |
| 35 |
), |
| 36 |
KeyBinding( |
| 37 |
binding1='Ctrl-q', |
| 38 |
description='Edit key bindings', |
| 39 |
method_name='edit_bindings', |
| 40 |
), |
| 41 |
) |
| 42 |
|
| 43 |
# ------------------------------------------------------------------------- |
| 44 |
# 'CodeHandler' class: |
| 45 |
# ------------------------------------------------------------------------- |
| 46 |
|
| 47 |
|
| 48 |
class CodeHandler(Handler): |
| 49 |
def save_file(self, info): |
| 50 |
info.object.status = "save file" |
| 51 |
|
| 52 |
def run_script(self, info): |
| 53 |
info.object.status = "run script" |
| 54 |
|
| 55 |
def edit_bindings(self, info): |
| 56 |
info.object.status = "edit bindings" |
| 57 |
key_bindings.edit_traits() |
| 58 |
|
| 59 |
|
| 60 |
# ------------------------------------------------------------------------- |
| 61 |
# 'TestCode' class: |
| 62 |
# ------------------------------------------------------------------------- |
| 63 |
|
| 64 |
|
| 65 |
class TestCode(HasPrivateTraits): |
| 66 |
|
| 67 |
code = Code() |
| 68 |
status = Str() |
| 69 |
|
| 70 |
view = View( |
| 71 |
[ |
| 72 |
Item( |
| 73 |
'code', |
| 74 |
style='custom', |
| 75 |
resizable=True, |
| 76 |
editor=CodeEditor(key_bindings=key_bindings), |
| 77 |
), |
| 78 |
'status~', |
| 79 |
'|<>', |
| 80 |
], |
| 81 |
id='traitsui.tests.test_code_editor.TestCode', |
| 82 |
title='Sample Code Editor', |
| 83 |
width=0.4, |
| 84 |
height=0.4, |
| 85 |
resizable=True, |
| 86 |
handler=CodeHandler(), |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
# ------------------------------------------------------------------------- |
| 91 |
# Run the test: |
| 92 |
# ------------------------------------------------------------------------- |
| 93 |
|
| 94 |
if __name__ == '__main__': |
| 95 |
TestCode().configure_traits() |