tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] the Monitor class not correctly initialize

Open parrotdance opened this issue 3 years ago • 5 comments

Describe the bug

const monitor = await currentMonitor()
if (monitor) {
  const { size, scaleFactor } = monitor
  size.toLogical(scaleFactor); // <--- [Error] toLogical does not exist on size
}

I have searched and read #2599 and #2621, and I think if we return a Monitor instance here:

/**
 * Returns the monitor on which the window currently resides.
 * Returns `null` if current monitor can't be detected.
 */
async function currentMonitor(): Promise<Monitor | null> {
  return invokeTauriCommand({
    __tauriModule: 'Window',
    message: {
      cmd: 'manage',
      data: {
        cmd: {
          type: 'currentMonitor'
        }
      }
    }
  })
} // .then(res => new Monitor( ... ))

this problem might be solved.

Reproduction

No response

Expected behavior

No response

Platform and versions

$ yarn tauri info
  › Node.js: 16.14.2
  › npm: 8.5.0
  › pnpm: Not installed!
  › yarn: 1.22.17
  › rustup: 1.24.3
  › rustc: 1.62.0
  › cargo: 1.62.0
  › Rust toolchain: stable-x86_64-pc-windows-msvc

Packages
  › @tauri-apps/cli [NPM]: 1.0.3
  › @tauri-apps/api [NPM]: 1.0.2
  › tauri [RUST]: 1.0.3,
  › tauri-build [RUST]: 1.0.3,
  › tao [RUST]: 0.12.1,
  › wry [RUST]: 0.19.0,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:3000/

App directory structure
  ├─ node_modules
  ├─ src
  └─ src-tauri
Done in 14.76s.

Stack trace

No response

Additional context

No response

parrotdance avatar Jul 14 '22 14:07 parrotdance

In fact I think we should not solve problems like this one-by-one.

There should be another layer like factory function to receive all return value of invokeTauriCommand and output a correctly initialized JS value.

Or find a way to convert tagged rust trait method into JS class method. Which looks not easy, and needs to consider about much of edge cases and constraints.

parrotdance avatar Jul 14 '22 14:07 parrotdance

I think it'll be easier to manage this in the function itself, converting the JSON object to the correct class.

lucasfernog avatar Jul 14 '22 15:07 lucasfernog

@lucasfernog but how to map a JSON object to the correct class? create a wrapper json object with the origin object and a class name identifier?

parrotdance avatar Jul 14 '22 15:07 parrotdance

The class interface should be adapted for each case. Same as #2621 we need to map the size and position fields of the Monitor to its classes.

lucasfernog avatar Jul 14 '22 16:07 lucasfernog

@lucasfernog I am not sure if I totally understand your opinion, but if you mean to use the same solution as #2621 to solve this problem, I will happy to try to handle this.

parrotdance avatar Jul 15 '22 00:07 parrotdance