ToggleButton (JavaScript) Taryn Fox jewelfox@fursona.net 2012 다시 클릭하기 전에는 눌린 상태를 유지합니다 조성호 shcho@gnome.org 2017 ToggleButton

ToggleButton은 보통 Button이지만 눌렀을 때 눌린 상태라는 점은 다릅니다. 이 예제에서 Spinner와 같은 매개를 다룰때 켬/끔 스위치로 사용할 수 있습니다.

ToggleButton의 get_active() 메서드는 눌렸을 때 true를 반환합니다. 그리고 눌린 상태가 아니면 false를 반환합니다. set_active() 메서드는 단추를 누르지 않아도 상태를 설정하고 싶을 때 사용합니다. 눌려있거나 튀어나왔거나 하는 상태를 바꿀 때, 어떤 함수와 연결할 수 있는 "toggled" 시그널을 내보냅니다.

가져올 라이브러리 #!/usr/bin/gjs const Gio = imports.gi.Gio; const Gtk = imports.gi.Gtk; const Lang = imports.lang;

이 프로그램을 실행할 때 가져올 라이브러리입니다. 시작 부분에 항상 gjs가 필요함을 알리는 줄을 작성해야 함을 기억하십시오.

프로그램 창 만들기 const ToggleButtonExample = new Lang.Class({ Name: 'ToggleButton Example', // Create the application itself _init: function() { this.application = new Gtk.Application({ application_id: 'org.example.jstogglebutton', flags: Gio.ApplicationFlags.FLAGS_NONE }); // Connect 'activate' and 'startup' signals to the callback functions this.application.connect('activate', Lang.bind(this, this._onActivate)); this.application.connect('startup', Lang.bind(this, this._onStartup)); }, // Callback function for 'activate' signal presents window when active _onActivate: function() { this._window.present(); }, // Callback function for 'startup' signal builds the UI _onStartup: function() { this._buildUI (); },

이 예제의 모든 코드는 RadioButtonExample 클래스에 들어갑니다. 위 코드는 위젯과 창이 들어가는 Gtk.Application을 만듭니다.

// Build the application's UI _buildUI: function() { // Create the application window this._window = new Gtk.ApplicationWindow({ application: this.application, window_position: Gtk.WindowPosition.CENTER, default_height: 300, default_width: 300, border_width: 30, title: "ToggleButton Example"});

_buildUI 함수는 프로그램 사용자 인터페이스를 만드는 모든 코드를 넣는 곳입니다. 첫 단계에서는 모든 위젯을 우겨넣을 새 Gtk.ApplicationWindow를 만듭니다.

ToggleButton과 다른 위젯 만들기 // Create the spinner that the button stops and starts this._spinner = new Gtk.Spinner ({hexpand: true, vexpand: true});

여기 Spinner를 가로 세로 망향으로 확장하여 창 안에서 최대한의 공간을 취할 수 있도록 하려고 합니다.

// Create the togglebutton that starts and stops the spinner this._toggleButton = new Gtk.ToggleButton ({label: "Start/Stop"}); this._toggleButton.connect ('toggled', Lang.bind (this, this._onToggle));

ToggleButton을 만드는 과정은 보통 Button을 만드는 과정과 비슷합니다. "clicked" 시그널이 아닌 "toggled" 시그널을 처리한다는게 큰 차이점입니다. 이 코드에서는 _onToggle 함수를 시그널에 연결하여 ToggleButton의 상태가 바뀌었을 때 호출합니다.

// Create a grid and put everything in it this._grid = new Gtk.Grid ({ row_homogeneous: false, row_spacing: 15}); this._grid.attach (this._spinner, 0, 0, 1, 1); this._grid.attach (this._toggleButton, 0, 1, 1, 1);

여기서 우리는 모든 위젯을 모아두는 간단한 Grid를 만들고 Spinner와 ToggleButton을 붙여두겠습니다.

// Add the grid to the window this._window.add (this._grid); // Show the window and all child widgets this._window.show_all(); },

이제 창에 Grid를 추가하고, 프로그램을 시작할 때 창 자체를 나타낸 다음 하위 위젯을 나타내라고 하겠습니다.

ToggleButton 상태가 바뀔때 동작 만들기 _onToggle: function() { // Start or stop the spinner if (this._toggleButton.get_active ()) this._spinner.start (); else this._spinner.stop (); } });

단추 눌린 상태를 누군가가 바꾸면, 이 함수에서 get_active() 함수를 사용하여 상태를 확인한 다음에 바로 스피너 동작을 시작하거나 멈춥니다. 우리는 단추가 눌린 동안에만 스피너가 돌아가게 하려고 하기에 get_active에서 true를 반환할 경우에 스피너 회전을 시작하겠습니다. 아니면 멈추라고 하고요.

// Run the application let app = new ToggleButtonExample (); app.application.run (ARGV);

마지막으로, 마무리 지은 RadioButtonExample 클래스의 새 인스턴스를 만들고 프로그램 실행을 설정하겠습니다.

완전한 코드 예제 #!/usr/bin/gjs imports.gi.versions.Gtk = '3.0'; const Gio = imports.gi.Gio; const Gtk = imports.gi.Gtk; class ToggleButtonExample { // Create the application itself constructor() { this.application = new Gtk.Application({ application_id: 'org.example.jstogglebutton', flags: Gio.ApplicationFlags.FLAGS_NONE }); // Connect 'activate' and 'startup' signals to the callback functions this.application.connect('activate', this._onActivate.bind(this)); this.application.connect('startup', this._onStartup.bind(this)); } // Callback function for 'activate' signal presents window when active _onActivate() { this._window.present(); } // Callback function for 'startup' signal builds the UI _onStartup() { this._buildUI(); } // Build the application's UI _buildUI() { // Create the application window this._window = new Gtk.ApplicationWindow({ application: this.application, window_position: Gtk.WindowPosition.CENTER, default_height: 300, default_width: 300, border_width: 30, title: "ToggleButton Example"}); // Create the spinner that the button stops and starts this._spinner = new Gtk.Spinner ({hexpand: true, vexpand: true}); // Create the togglebutton that starts and stops the spinner this._toggleButton = new Gtk.ToggleButton ({label: "Start/Stop"}); this._toggleButton.connect ('toggled', this._onToggle.bind(this)); // Create a grid and put everything in it this._grid = new Gtk.Grid ({ row_homogeneous: false, row_spacing: 15}); this._grid.attach (this._spinner, 0, 0, 1, 1); this._grid.attach (this._toggleButton, 0, 1, 1, 1); // Add the grid to the window this._window.add (this._grid); // Show the window and all child widgets this._window.show_all(); } _onToggle() { // Start or stop the spinner if (this._toggleButton.get_active ()) this._spinner.start (); else this._spinner.stop (); } }; // Run the application let app = new ToggleButtonExample (); app.application.run (ARGV);
자세한 문서

Gtk.Application

Gtk.ApplicationWindow

Gtk.Grid

Gtk.Spinner

Gtk.ToggleButton