Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GTK4 List API examples with filters and sort #157

Open
gavr123456789 opened this issue Jul 18, 2021 · 6 comments
Open

Add GTK4 List API examples with filters and sort #157

gavr123456789 opened this issue Jul 18, 2021 · 6 comments

Comments

@gavr123456789
Copy link
Author

Sort blocked by #144
String Sorter can only work with expressions.

@gavr123456789
Copy link
Author

@gavr123456789
Copy link
Author

gavr123456789 commented Jul 19, 2021

@gavr123456789
Copy link
Author

Asked for help here: https://discourse.gnome.org/t/how-to-reset-filefilter-and-how-filefilter-add-pattern-works/6996

Problem with FileFilter was solved.

@gavr123456789
Copy link
Author

I think this is very close, but FileInfo has no propertiess, so need to use Expression with GClosure https://developer.gnome.org/gtk4/stable/GtkExpression.html#gtk-closure-expression-new

import gintro/[gtk4, gobject, gio]
import std/with

proc getFileName(info: gio.FileInfo): string =
  return info.getName()  

proc setup_cb(factory: gtk4.SignalListItemFactory, listitem: gtk4.ListItem) =
  listitem.setChild(newLabel(""))
  
proc bind_cb(factory: gtk4.SignalListItemFactory, listitem: gtk4.ListItem) =
  let 
    lb = listitem.getChild().Label
    fileobj = cast[FileInfo](listitem.getItem())
  
  lb.text = fileobj.getFileName()

proc unbind_cb(factory: gtk4.SignalListItemFactory, listitem: gtk4.ListItem) =
  # There's nothing to do here. 
  # If you does something like setting a signal in bind_cb,
  # then disconnecting the signal is necessary in unbind_cb. 
  echo "unbind"

proc teardown_cb(factory: gtk4.SignalListItemFactory, listitem: gtk4.ListItem) =
  listitem.setChild (nil)
  # When the child of listitem is set to NULL, the reference to GtkLabel will be released and lb will be destroyed. 
  # Therefore, g_object_unref () for the GtkLabel object doesn't need in the user code. 

proc entryChanged(self: EntryBuffer, position: int, chars: string, nChars: int) = 
  echo self.text


proc activate(app: gtk4.Application) =
  let
    window = newApplicationWindow(app)
    scr = newScrolledWindow()
    file = gio.newGFileForPath(".")
    dl = gtk4.newDirectoryList("standard::name", file)
    ls = listModel(dl)
    ss = newStringSorter()
    fm = newSortListModel(ls, ss)
    ns = gtk4.newNoSelection(listModel(fm))
    factory = gtk4.newSignalListItemFactory()
    lv = newListView(ns, factory)

    mainBox = newBox(Orientation.vertical, 5)
    patternEntry = newEntry()


  patternEntry.placeholderText = "Enter pattern"
  patternEntry.buffer.connect("inserted_text", entryChanged)



  with mainBox:
    append patternEntry
    append scr
  let gFileType = typeFromName("GFileInfo")
  ss.expression = newPropertyExpression(gFileType, nil, "display_name" )
  

  with scr: 
    vexpand = true
    setChild lv

  dl.setMonitored true

  with factory:
    connect("setup", setup_cb)
    connect("bind", bind_cb)
    connect("unbind", unbind_cb)
    connect("teardown", teardown_cb)

  with window:
    defaultSize = (600, 400)
    title = "Nim ListView"
    setChild mainBox
    show

proc main =
  let app = newApplication("org.gtk.example")
  app.connect("activate", activate)
  discard run(app)

main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant