Skip to content

Commit

Permalink
options: Implement nicer output directory chooser
Browse files Browse the repository at this point in the history
related to #2
  • Loading branch information
rafaelmardojai committed May 27, 2020
1 parent c627f50 commit 161d0ac
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 17 deletions.
21 changes: 13 additions & 8 deletions src/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

from gettext import gettext as _
from gi.repository import GLib, GObject, Gio, Gtk, Handy
from fontTools.subset import parse_unicodes
Expand All @@ -24,7 +26,8 @@
class Options(Gtk.Box):
__gtype_name__ = 'Options'

directory_row = Gtk.Template.Child()
browse = Gtk.Template.Child()
directory = Gtk.Template.Child()

# Fonts format
format_woff2 = Gtk.Template.Child()
Expand Down Expand Up @@ -54,13 +57,7 @@ def __init__(self, **kwargs):
self.load_saved()

def setup(self):
# Setup directory file chooser button
self.directory = Gtk.FileChooserButton.new(
_('Select output directory'),
Gtk.FileChooserAction.SELECT_FOLDER)
self.directory.props.valign = Gtk.Align.CENTER
self.directory.show_all()
self.directory_row.add(self.directory)
self.directory.connect('changed', self._check_dir_entry)

# Setup font_display combo row
model = Gio.ListStore.new(Handy.ValueObject)
Expand Down Expand Up @@ -146,3 +143,11 @@ def get_subsetting(self):
def get_font_display(self):
return self.font_display.get_selected_index()


def _check_dir_entry(self, entry):
path = entry.get_text()
if os.access(path, os.W_OK) and os.path.exists(path):
entry.get_style_context().remove_class('error')
else:
entry.get_style_context().add_class('error')

55 changes: 52 additions & 3 deletions src/resources/ui/options.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,64 @@
<property name="margin_top">40</property>
<property name="margin_bottom">40</property>
<child>
<object class="HdyPreferencesGroup">
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Output directory</property>
<property name="valign">center</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<object class="HdyActionRow" id="directory_row">
<property name="title" translatable="yes">Output directory</property>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="valign">center</property>
<style>
<class name="linked"/>
</style>
<child>
<object class="GtkEntry" id="directory">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="placeholder-text" translatable="yes">Select a directory for generation</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<object class="GtkButton" id="browse">
<property name="label" translatable="yes">Browse...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
</packing>
</child>

<child>
Expand Down
33 changes: 27 additions & 6 deletions src/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ def __init__(self, **kwargs):

self.processing = False
self.options = Options()
self.outpath = None
self.outpath = ''
self.log = Log(self.progressbar_label)

self.setup_widgets()

self.options.directory.connect('file-set', self._update_outpath)
self.options.directory.connect('file-set', self._change_ready_state)
self.options.browse.connect('clicked', self.set_outpath)
self.options.directory.connect('changed', self._update_outpath)
self.options.directory.connect('changed', self._change_ready_state)

self.model = Gio.ListStore.new(Font)
self.model.connect('items-changed', self._change_ready_state)
Expand Down Expand Up @@ -141,6 +142,21 @@ def on_generate(self, widget):
self.options.get_font_display())
generator.run()

def set_outpath(self, *args):
filechooser = Gtk.FileChooserNative.new(
_('Select output folder'),
self,
Gtk.FileChooserAction.SELECT_FOLDER,
None,
None)
response = filechooser.run()

if response == Gtk.ResponseType.ACCEPT:
path = filechooser.get_filename()
self.options.directory.set_text(path)

elif response == Gtk.ResponseType.REJECT:
filechooser.destroy()

'''
PRIVATE
Expand All @@ -150,13 +166,13 @@ def _create_font_widget(self, font):
widget = FontWidget(font, self.model)
return widget

def _update_outpath(self, chooser):
self.outpath = chooser.get_filename()
def _update_outpath(self, entry):
self.outpath = entry.get_text()

def _change_ready_state(self, *args, **kwargs):
children = True if len(self.model) > 0 else False

if children and self.outpath:
if children and self._check_path_access(self.outpath):
self.btn_generate.set_sensitive(True)
else:
self.btn_generate.set_sensitive(False)
Expand All @@ -166,6 +182,11 @@ def _change_ready_state(self, *args, **kwargs):
else:
self.fonts_stack.set_visible_child_name('empty')

def _check_path_access(self, path):
if os.access(path, os.W_OK) and os.path.exists(path):
return True
return False

def _get_font_data(self, data_src):
data = {}
weights = {
Expand Down

0 comments on commit 161d0ac

Please sign in to comment.